Question

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

@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().

Then in onCreate() method, I write following code:

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.

Was it helpful?

Solution

Remove super.onDestroy() from onPause() method and add super.onPause() as below...

@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. 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...

java.lang.IllegalStateException: No activity
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top