2

Ich arbeite mit SD-Karte und so versucht, Erlaubnis in der Laufzeit zu erhalten. Hier ist der Code:Android java.lang.IllegalStateException in onRequestPermissionsResult()

public class MainActivity extends AppCompatActivity implements FileListFragment.OnFragmentInteractionListener { 

private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 111; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 
     } 

     ................... 
     ................ 
    } 

@Override 
    public void onRequestPermissionsResult(int requestCode, 
              String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        FileListFragment fileListFragment = FileListFragment.newInstance(0); // **Error line** 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment_container, fileListFragment) 
       .commit(); 
       } else { 

        finish(); 
       } 
       return; 
      } 
     } 
    } 

............................. 
.................................. 
.......................... 

} 

Sobald ich damit bin die Erlaubnis, es „Failure liefern Ergebnis result wirft {wer = @ android: requestPermissions :, request = 111, Ergebnis = -1, data = Intent {act = android.content.pm.action.REQUEST_PERMISSIONS (hat Extras)}} zur Aktivität {com.kaushik.fileexplorer/com.kaushik.fileexplorer.MainActivity}: java.lang.IllegalStateException: Diese Aktion kann nach onSaveInstanceState nicht ausgeführt werden ". Hier ist die Details Logcat:

08-23 16:49:29.497 3215-3215/com.kaushik.fileexplorer E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.kaushik.fileexplorer, PID: 3215 
                     java.lang.RuntimeException: Failure delivering result ResultInfo{[email protected]:requestPermissions:, request=111, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.kaushik.fileexplorer/com.kaushik.fileexplorer.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
                      at android.app.ActivityThread.deliverResults(ActivityThread.java:3699) 
                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                      at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                     Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
                      at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1533) 
                      at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1551) 
                      at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:696) 
                      at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:662) 
                      at com.kaushik.fileexplorer.MainActivity.onRequestPermissionsResult(MainActivity.java:76) 
                      at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6553) 
                      at android.app.Activity.dispatchActivityResult(Activity.java:6432) 
                      at android.app.ActivityThread.deliverResults(ActivityThread.java:3695) 
                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)  
                      at android.app.ActivityThread.-wrap16(ActivityThread.java)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)  
                      at android.os.Handler.dispatchMessage(Handler.java:102)  
                      at android.os.Looper.loop(Looper.java:148)  

Was ist das Problem in diesem Code?

+0

Fügen Sie Ihren 'onSaveInstanceState' Code hinzu? –

+0

Haben Sie die Berechtigung in 'manifest' Datei hinzugefügt? Sie können nicht zur Laufzeit um Erlaubnis bitten, wenn Sie dies nicht tun. – Abbas

+0

@SohailZahid Ich habe onSaveInstanceState() überhaupt nicht überschrieben. –

Antwort

5

Sie sollten commitAllowingStateLoss hinzufügen()

FileListFragment fileListFragment = FileListFragment.newInstance(0); // **Error line** 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment_container, fileListFragment) 
       .commitAllowingStateLoss(); 

Bitte überprüfen this link

3

Dieser Fehler tritt auf, weil der Aktivitätsstatus nicht gespeichert wird und Sie Fragment davor hinzufügen.

Verwendung Dieser Code:

FileListFragment fileListFragment = FileListFragment.newInstance(0); 
    getSupportFragmentManager().beginTransaction() 
      .add(R.id.fragment_container, fileListFragment) 
      .commitAllowingStateLoss(); 

und überschreiben Sie Ihre onSaveInstanceState() Verfahren und entfernen Sie die super() von ihm.

0

commitAllowingStateLoss mit() eher wie ein Hacky fix ist.

Dieses Problem ist diesem sehr ähnlich und wird gut erklärt here.

Stattdessen ist es besser, ein boolesches Flag in onRequestPermissionsResult zu setzen und dieses in onPostResume (für Activities) oder onResume (für Fragments) zu verwenden, um die Transaktion zu committen.

Verwandte Themen