Android片段仅在手机被锁定时才会提交非法状态异常

时间:2014-06-23 05:33:15

标签: android android-fragments illegalstateexception android-actionbaractivity

我正在接收广播片段,例如

 BroadcastReceiver br = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Log.e(TAG,"inside onReceive");
            if (intent.getAction().equalsIgnoreCase(
                    DATA_AVAILABLE))
                initFragments.run();
                }
     }

InitFragment是一个带代码的可运行的

dataBrowse = new SongListFragment();
                getSupportFragmentManager().beginTransaction()
                        ./*replace*/ add (R.id.container, dataBrowse ).commit();

除了手机锁定外,它始终完美无缺。为什么会这样?这个原因的解决方案是什么?

日志:

06-23 03:56:38.210: E/AndroidRuntime(11174): FATAL EXCEPTION: main
06-23 03:56:38.210: E/AndroidRuntime(11174): java.lang.RuntimeException: Error receiving broadcast Intent { act=dat_avail flg=0x10 } in com.example.MainActivity$1@41b98e90
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.os.Handler.handleCallback(Handler.java:615)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.os.Looper.loop(Looper.java:137)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.app.ActivityThread.main(ActivityThread.java:4898)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at java.lang.reflect.Method.invokeNative(Native Method)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at java.lang.reflect.Method.invoke(Method.java:511)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at dalvik.system.NativeStart.main(Native Method)
06-23 03:56:38.210: E/AndroidRuntime(11174): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at com.example.MainActivity$3.run(MainActivity.java:228)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at com.example.MainActivity$1.onReceive(MainActivity.java:101)
06-23 03:56:38.210: E/AndroidRuntime(11174):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
06-23 03:56:38.210: E/AndroidRuntime(11174):    ... 9 more
06-23 03:56:38.245: I/Process(11174): Sending signal. PID: 11174 SIG: 9

1 个答案:

答案 0 :(得分:3)

我认为您的程序中存在状态丢失,其中一个解决方案是保存实例并恢复避免状态丢失的情况,另一个解决方案是允许状态丢失,您可以查看blogspot {{ 3}}