Home » Android » Error : [email protected] is not valid; is your activity running?

Error : [email protected] is not valid; is your activity running?

Posted by: admin November 30, 2017 Leave a comment

Questions:

What is this error… i haven’t found any discussion on this error in the stackoverflow community Detailed :-

10-18 23:53:11.613: ERROR/AndroidRuntime(3197): Uncaught handler: thread main exiting due to uncaught exception
10-18 23:53:11.658: ERROR/AndroidRuntime(3197): android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.view.ViewRoot.setView(ViewRoot.java:468)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.app.Dialog.show(Dialog.java:239)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at com.vishal.contacte.Locationlistener$MyLocationListener.onLocationChanged(Locationlistener.java:86)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:179)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:112)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:128)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.os.Looper.loop(Looper.java:123)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at java.lang.reflect.Method.invokeNative(Native Method)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at java.lang.reflect.Method.invoke(Method.java:521)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
10-18 23:53:11.658: ERROR/AndroidRuntime(3197):     at dalvik.system.NativeStart.main(Native Method)
Answers:

This is most likely happening because you are trying to show a dialog after execution of a background thread, while the Activity is being destroyed.

I was seeing this error reported once in a while from some of my apps when the activity calling the dialog was finishing for some reason or another when it tried to show a dialog. Here’s what solved it for me:

if(!((Activity) context).isFinishing())
{
    //show dialog
}

I’ve been using this to work around the issue on older versions of Android for several years now, and haven’t seen the crash since.

Questions:
Answers:

check this link:
Android – Displaying Dialogs From Background Threads
http://dimitar.me/android-displaying-dialogs-from-background-threads/

Questions:
Answers:

I faced the same problem and used the code proposed by DiscDev above with minor changes as follows:

if (!MainActivity.this.isFinishing()){
    alertDialog.show();
}

Questions:
Answers:

I encountered this error when I had a countDownTimer in my app. It had a method calling GameOver in my app as

public void onFinish() {
     GameOver();
}

but actually the game could be over before the time was up due to a wrong click of the user (it was a clicking game). So when I was looking at the Game Over dialog after e.g. 20 seconds, I forgot to cancel the countDownTimer so once the time was up, the dialog appeared again. Or crashed with the above error for some reason.

Questions:
Answers:

The fix to this is pretty simple. Just test if the Activity is going through its finishing phase before displaying the Dialog:

  private Handler myHandler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    switch (msg.what) {
      case DISPLAY_DLG:
        if (!isFinishing()) {
        showDialog(MY_DIALOG);
        }
      break;
    }
  }
};

see more here

Questions:
Answers:

In my case the problem was that Context was kept as a weak reference in the class that extends Handler. Then I was passing Messenger, that wraps the handler, through an Intent to a Service. I was doing this each time activity appeared on screen in onResume() method.

So as you understand, messenger was serialized together with its fields (including context), because it is the only way to pass objects using Intent – to serialize them. And at that moment when messenger was passed to the service, activity itself still wasn’t ready for showing dialogs as it is in another state (being said onResume(), that is absolutely different from when activity is already on the screen). So when messenger was deserialized, the context still was at the resuming state, while the activity actually was already on the screen. Moreover deserialization allocates memory for new object, that is completely different from the original one.

So, the solution is just to bind to the service each time you need it and return binder that has a method like ‘setMessenger(Messenger messenger)’ and call it, when you are binded to the service.