IllegalStateException:无法在FragmentActivity类中的onSaveInstanceState之后执行此操作

时间:2013-08-19 05:05:49

标签: android android-fragments android-fragmentactivity

这是我写的代码。我无法找到此功能何时崩溃但有时可以正常工作。

public class ListFragmentActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
  main();
  IntentFilter filter1 = new IntentFilter("com.venkat.listfragment");
  registerReceiver(myReceiver, filter1);
  // register reciever intent filter.

}
public void main(){
    IntentFilter filter1 = new IntentFilter("com.venkat.listfragment");
    registerReceiver(myReceiver, filter1);
     List_Fragment ls_fragment = new List_Fragment();
     getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
     getSupportFragmentManager().beginTransaction()
     .replace(android.R.id.content, ls_fragment).addToBackStack(null).commitAllowingStateLoss();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
}
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent) {
        main();

    }
};}

我有四个选项卡,当我点击一个选项卡时,它会显示相应的屏幕,在该屏幕上,我在同一个选项卡中导航另一个屏幕。但是当我点击相同的选项卡时我回到上一个屏幕写了一个广播接收器。

我收到此错误:

08-19 10:13:51.436: E/AndroidRuntime(2830): FATAL EXCEPTION: main
08-19 10:13:51.436: E/AndroidRuntime(2830): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.venkat.listfragment } in com.iheartsalesevent.main.ListFragmentActivity$1@44a89218
08-19 10:13:51.436: E/AndroidRuntime(2830):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:905)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at android.os.Handler.handleCallback(Handler.java:587)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at android.os.Looper.loop(Looper.java:123)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at java.lang.reflect.Method.invoke(Method.java:521)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at dalvik.system.NativeStart.main(Native Method)
08-19 10:13:51.436: E/AndroidRuntime(2830): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
08-19 10:13:51.436: E/AndroidRuntime(2830):     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1354)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:493)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at com.venkat.main.ListFragmentActivity.main(ListFragmentActivity.java:65)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at com.venkat.main.ListFragmentActivity$1.onReceive(ListFragmentActivity.java:89)
08-19 10:13:51.436: E/AndroidRuntime(2830):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:892)
08-19 10:13:51.436: E/AndroidRuntime(2830):     ... 9 more

我提到了很多与此问题相关的链接,但这些都没有解决我的问题。

1 个答案:

答案 0 :(得分:6)

onResume()中注册BroadcastReceiver并在onPause()中取消注册将解决此问题。如果需要,您也可以使用onStart() / onStop()