[英]IllegalStateException: No activity when onStop() method called
In the beginning i define three integers 一开始我定义了三个整数
public static int button1, buttoncos, buttonmadd;
Then i store the value of drawables in them 然后我将可绘制对象的值存储在其中
case R.id.blue:
for (Button currentButton : buttons) {
currentButton.setBackgroundResource(R.drawable.blue);
button1 = buttoncos = buttonmadd = R.drawable.blue;
};
return true;
Then in onDestroy()
and onPause()
, I write following code 然后在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();
}
I write nothing in onStart()
. 我没有在onStart()
写任何东西。
Then in onCreate()
method, I write following code: 然后在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);
After i run my application and change themes my app crashes. 运行我的应用程序并更改主题后,我的应用程序崩溃了。 Here is my 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
Why am i getting this error. 为什么我会收到此错误。
Remove super.onDestroy()
from onPause()
method and add super.onPause()
as below... 从onPause()
方法中删除super.onDestroy()
,并如下所示添加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();
}
Since you calling onDestroy()
method of super
class in onPause()
method then the Activity
finishes in before onStop()
method. 由于您在onPause()
方法中调用了super
类的onDestroy()
方法,因此Activity
在onStop()
方法之前完成。 So, when onStop()
method is called according to the activity life-cycle then it can't find any activity...That's why following Exception
causing... 因此,当根据活动生命周期调用onStop()
方法时,它将找不到任何活动...这就是跟随Exception
导致的原因...
java.lang.IllegalStateException: No activity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.