gpt4 book ai didi

java - IllegalStateException:调用 onStop() 方法时无 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:54 24 4
gpt4 key购买 nike

一开始我定义了三个整数

public static int button1, buttoncos, buttonmadd;

然后我将可绘制对象的值存储在其中

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

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

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

为什么会出现此错误。

最佳答案

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

由于您在 onPause() 方法中调用了 super 类的 onDestroy() 方法,因此 Activity 完成在 onStop() 方法之前。因此,当根据 Activity 生命周期调用 onStop() 方法时,它找不到任何 Activity ...这就是为什么以下 Exception 导致...

java.lang.IllegalStateException: No activity

关于java - IllegalStateException:调用 onStop() 方法时无 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com