简体   繁体   English

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

[英]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()方法,因此ActivityonStop()方法之前完成。 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.

相关问题 重新启动活动时调用onStop延迟 - onStop delays to be called when restarting activity 在调用onStop()之后导航到活动 - Navigating to activity after onStop() is called RecyclerView在触发activity onStop时释放MediaPlayer - RecyclerView release MediaPlayer when activity onStop is triggered 当没有调用“匹配”方法时,匹配器抛出 IllegalStateException 的基本原理 - Rationale for Matcher throwing IllegalStateException when no 'matching' method is called 如何在不调用onPause和onStop的情况下调用destroy()方法 - How to call on destroy() method without being called onPause & onStop Android-IllegalStateException:无法执行活动的方法 - Android - IllegalStateException: could not execute method of the activity 发现按钮IllegalStateException:无法执行活动的方法 - Discover button IllegalStateException: Could not execute method of the activity Android Java IllegalStateException:无法执行活动的方法 - Android Java IllegalStateException: Could not execute method of the activity 用户关闭监视器时调用什么活动生命周期方法 - What activity lifecycle method is called when the user switches off the monitor FLUME IllegalStateException:当事务为OPEN时调用begin() - FLUME IllegalStateException: begin() called when transaction is OPEN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM