IllegalStateException:调用onStop()方法时没有活动

时间:2014-03-17 18:30:46

标签: java android illegalstateexception

一开始我定义了三个整数

public static int button1, buttoncos, buttonmadd;

然后我将drawables的值存储在其中

case R.id.blue:
          for (Button currentButton : buttons) {
                currentButton.setBackgroundResource(R.drawable.blue);
                button1 = buttoncos = buttonmadd = R.drawable.blue;
            };
          return true;

然后在onDestroy()onPause()中,我编写了以下代码

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    editor = preferences.edit();
    editor.putInt("DigitButtonStyle",button1);
    editor.putInt("MemoryButtonStyle", buttonmadd);
    editor.putInt("FunctionButtonStyle", buttoncos);
    editor.commit();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onDestroy();
    editor = preferences.edit();
    editor.putInt("DigitButtonStyle",button1);
    editor.putInt("MemoryButtonStyle", buttonmadd);
    editor.putInt("FunctionButtonStyle", buttoncos);
    editor.commit();
}

我在onStart()中没有写任何内容。

然后在onCreate()方法中,我编写以下代码:

for (Button currentButton : digitbuttons) {
        currentButton.setBackgroundResource(button1);
    }
  for (Button currentButton : memoryfunctions) {
        currentButton.setBackgroundResource(buttonmadd);
    }
  for (Button currentButton : functionbuttons) {
        currentButton.setBackgroundResource(buttoncos);
    }
    }
preferences = PreferenceManager.getDefaultSharedPreferences(this);

运行我的应用程序并更改主题后,我的应用程序崩溃了。这是我的logcat:

03-17 23:53:38.033: E/AndroidRuntime(15638): FATAL EXCEPTION: main
03-17 23:53:38.033: E/AndroidRuntime(15638): java.lang.RuntimeException: Unable to stop activity {com.example.calculator/com.example.calculator.MainActivity}: java.lang.IllegalStateException: No activity
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3625)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3679)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.app.ActivityThread.access$1200(ActivityThread.java:162)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1417)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.os.Handler.dispatchMessage(Handler.java:107)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.os.Looper.loop(Looper.java:194)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.app.ActivityThread.main(ActivityThread.java:5371)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at java.lang.reflect.Method.invokeNative(Native Method)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at java.lang.reflect.Method.invoke(Method.java:525)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at dalvik.system.NativeStart.main(Native Method)
03-17 23:53:38.033: E/AndroidRuntime(15638): Caused by: java.lang.IllegalStateException: No activity
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1091)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.support.v4.app.FragmentManagerImpl.dispatchStop(FragmentManager.java:1907)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.support.v4.app.FragmentActivity.onStop(FragmentActivity.java:612)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.support.v7.app.ActionBarActivity.onStop(ActionBarActivity.java:109)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1211)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.app.Activity.performStop(Activity.java:5264)
03-17 23:53:38.033: E/AndroidRuntime(15638):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3620)
03-17 23:53:38.033: E/AndroidRuntime(15638):    ... 11 more

为什么我会收到此错误。

1 个答案:

答案 0 :(得分:3)

super.onDestroy()方法移除onPause()并添加super.onPause(),如下所示...

@Override
protected void onPause() {

    super.onPause();
    editor = preferences.edit();
    editor.putInt("DigitButtonStyle",button1);
    editor.putInt("MemoryButtonStyle", buttonmadd);
    editor.putInt("FunctionButtonStyle", buttoncos);
    editor.commit();
}

由于您在onDestroy()方法中调用super类的onPause()方法,因此ActivityonStop()方法之前完成。因此,当根据活动生命周期调用onStop()方法时,它无法找到任何活动......这就是导致Exception导致...

java.lang.IllegalStateException: No activity