Error: BinderProxy @ 45d459c0 is not valid; Does your activity work?

What is this error ... I did not find a discussion of this error in the stackoverflow community. Details: -

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 android.os.BinderProxy@45d459c0 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) 
+125
android
18 Oct. '11 at 18:32
source share
11 answers

This is most likely because you are trying to show a dialog after the background thread is executed, and the activity is destroyed.

I saw that this error was reported once in a while from some of my applications, when the activity that caused the dialog ended for one reason or another, when it tried to show the dialog. Here is what solved this for me:

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

I have been using this to get around this problem in older versions of Android for several years, and have not seen a crash since.

+303
Jan 23 '13 at
source share

check this link:
Android - Display dialogs from background threads

+21
Feb 16 2018-12-12T00:
source share

I ran into the same problem and used the code suggested by DiscDev above, with slight modifications as follows:

 if (!MainActivity.this.isFinishing()){ alertDialog.show(); } 
+9
Jul 04 '17 at 13:05
source share

I ran into this error when I had countDownTimer in my application. He had a method calling GameOver in my application as

 public void onFinish() { GameOver(); } 

but in fact, the game can end before the time is over due to a wrong click of the user (it was a game with a click). Therefore, when I watched the Game Over dialog, for example, 20 seconds, I forgot to cancel countDownTimer , so once the time was up, the dialog appeared again. Or for some reason with the error above.

+2
Dec 24 '15 at 11:58
source share

Fixing this is pretty simple. Just check if the Activity passes at its final stage 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; } } }; 

more details here

+1
Aug 05 '17 at 1:22 on
source share

In my case, the problem was that Context remained a weak reference in the class extending Handler . Then I passed the Messenger , which wraps the handler, through Intent to Service . I did this every time an activity appeared on the screen in onResume() .

So, as you understand, Messenger was serialized along with its fields (including context), because this is the only way to transfer objects using Intent - their serialization. The moment the messenger was transferred to the service, the action itself was still not ready to display dialogs, since it was in a different state (as stated in Resume (), which is completely different from when the action is already on the screen). Therefore, when the messenger was deserialized, the context was still in a state of renewal, while the action was actually already on the screen. In addition, deserialization allocates memory for a new object that is completely different from the original.

The solution is to simply contact the service every time you need it and return a binder that has a method such as 'setMessenger (Messenger messenger)' and call it when you are bound to the service.

+1
Oct 30 '17 at 23:25
source share

if the dialog causes this problem due to a thread, you should do it in the user interface thread as follows:

 runOnUiThread(new Runnable() { @Override public void run() { dialog.show(); } }); 
+1
Jun 06 '18 at 5:55
source share

I have WeakReference<Activity> this problem, using WeakReference<Activity> as the context. The wreck never appeared again. Here is an example code in Kotlin:

Dialog class manager:

 class DialogManager { fun showAlertDialog(weakActivity: WeakReference<Activity>) { val wActivity = weakActivity.get() wActivity?.let { val builder = AlertDialog.Builder(wActivity, R.style.MyDialogTheme) val inflater = wActivity.layoutInflater val dialogView = inflater.inflate(R.layout.alert_dialog_info, null) builder.setView(dialogView) // Set your dialog properties here. Eg builder.setTitle("MyTitle") builder.create().show() } } } 

And you show the dialog as follows:

  val dialogManager = DialogManager() dialogManager.showAlertDialog(WeakReference<Activity>(this@MainActivity)) 

If you want to be super-protected from crashes. Instead of builder.create().show() use:

 val dialog = builder.create() safeShow(weakActivity, dialog) 

This is the safeShow method:

 private fun safeShow(weakActivity: WeakReference<Activity>, dialog: AlertDialog?) { val wActivity = weakActivity.get() if (null != dialog && null != wActivity) { // Api >=17 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { if (!dialog.isShowing && !(wActivity).isFinishing && !wActivity.isDestroyed) { try { dialog.show() } catch (e: Exception) { //Log exception } } } else { // Api < 17. Unfortunately cannot check for isDestroyed() if (!dialog.isShowing && !(wActivity).isFinishing) { try { dialog.show() } catch (e: Exception) { //Log exception } } } } } 

This is a similar method that you can use to safely close the dialog box:

 private fun safeDismissAlertDialog(weakActivity: WeakReference<Activity>, dialog: AlertDialog?) { val wActivity = weakActivity.get() if (null != dialog && null != wActivity) { // Api >=17 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { if (dialog.isShowing && !wActivity.isFinishing && !wActivity.isDestroyed) { try { dialog.dismiss() } catch (e: Exception) { //Log exception } } } else { // Api < 17. Unfortunately cannot check for isDestroyed() if (!dialog.isShowing && !(wActivity).isFinishing) { try { dialog.dismiss() } catch (e: Exception) { //Log exception } } } } } 
+1
Nov 01 '18 at 7:45
source share

This error occurs when you display a dialog for a context that no longer exists.

Before calling .show() make sure the action / context does not end

 if (!(context instanceof Activity && ((Activity) context).isFinishing())) { alert.show(); } 
0
Nov 19 '18 at 10:17
source share

In my case, I forgot to comment on finish ();

0
Apr 13 '19 at 17:21
source share

how about creating a new instance of this dialog that you want to call? I actually just met the same problem, and that is what I am doing. so, not:

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

how about this?

  YourDialog mDialog = new YourDialog(); mDialog1.show(((AppCompatActivity) mContext).getSupportFragmentManager(), "OrderbookDialog"); } 

so instead of just checking whether itโ€™s safe or not to show the dialog, I think itโ€™s much safer if we just create a new instance to show the dialog.

Like me, in my case I tried to create one instance (from the onCreate fragment ) and call an instance of this dialog box in the other contents of the adapter, and this will lead to the error "your activity is running" - an error . I thought this was because I simply created one instance (from onCreate) and then it is destroyed, so when I tried to call it from another adapter, I called up a dialog from the old instance.

I'm not sure if my solution is memory friendly or not, because I did not try to profile it, but it works (of course, it is safe if you do not create too many instances)

0
Apr 30 '19 at 16:42
source share



All Articles