3

我正在使用 sd 卡,因此试图在运行时获得许可。这是代码:

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;
            }
        }
    }

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

}

一旦我允许权限,它就会抛出“失败交付结果 ResultInfo{who=@android:requestPermissions:, request=111, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (有额外的)}}活动{com.kaushik.fileexplorer/com.kaushik.fileexplorer.MainActivity}:java.lang.IllegalStateException:在onSaveInstanceState之后无法执行此操作“。这是详细信息日志:

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{who=@android: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) 

该代码有什么问题?

4

3 回答 3

5

您应该添加 commitAllowingStateLoss()

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

请检查此链接

于 2016-08-23T14:04:26.000 回答
3

此错误是因为未保存活动状态并且您在其之前添加了片段。

使用此代码:

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

并覆盖您的onSaveInstanceState()方法并从中删除super()

于 2016-08-23T13:58:16.270 回答
2

使用 commitAllowingStateLoss() 更像是一个 hacky 修复。

这个问题与这个问题非常相似,这里有很好的解释

相反,最好设置一个布尔标志onRequestPermissionsResult并在onPostResume(用于活动)或onResume(用于片段)中使用它来提交事务。

于 2017-09-21T13:04:43.157 回答