locked
Error using Dialog.Show on Samsung devices RRS feed

  • Question

  • User171101 posted

    Hello everyone,

    I've implemented with the Dependency Service a Custom loading overlay (using Dialog in android). It works rather wall except that:

    I'm receiving some errors now and then for Users with a Samsung device:

    After trying numerous things to reproduce / solve the problem on other devices without success I'm hoping someone can shed some light on the matter.

    The error I'm receiving:

    Java.Lang.RuntimeExceptionUnable to add window -- token android.os.BinderProxy@39038183 is not valid; is your activity running?
    Raw
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    Java.Interop.JniEnvironment.InstanceMethods.CallVoidMethod(JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args)
    Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(string encodedMember, IJavaPeerable self, JniArgumentValue* parameters)
    Android.App.Dialog.Show()
    B2B.Droid.LoadingService.ShowLoading()
    Java.Lang.Thread.RunnableImplementor.Run()
    Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this)
    at (wrapper dynamic-method) System.Object:9acf5e7c-63fb-4e3f-8148-957db60a2b2b (intptr,intptr)
    --- End of managed exception stack trace ---
    android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@39038183 is not valid; is your activity running?
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:785)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:298)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    at android.app.Dialog.show(Dialog.java:325)
    mono.java.lang.RunnableImplementor.n_run(Native Method)
    at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
    at android.app.Activity.runOnUiThread(Activity.java:5815)
    md5b60ffeb829f638581ab2bb9b1a7f4f3f.ButtonRenderer_ButtonClickListener.n_onClick(Native Method)
    at md5b60ffeb829f638581ab2bb9b1a7f4f3f.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:30)
    at android.view.View.performClick(View.java:5246)
    at android.widget.TextView.performClick(TextView.java:10602)
    at android.view.View$PerformClick.run(View.java:21256)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6917)
    java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
    

    The 'problematic' code:

    public class LoadingService : ILoadingService
    {
        TextView textView;
        Dialog dlg;
    
        public LoadingService()
        {
            LayoutInflater inflater = LayoutInflater.From (Forms.Context);
    
            Android.Views.View toaster = inflater.Inflate (Resource.Layout.loadingView, null);
    
            textView = toaster.FindViewById<TextView> (Resource.Id.textView);
            textView.SetTextSize (Android.Util.ComplexUnitType.Pt, 10f);
    
            dlg = new Dialog(Forms.Context);
    
            dlg.RequestWindowFeature((int)WindowFeatures.NoTitle);
            dlg.SetContentView (toaster);
    
            dlg.Window.SetBackgroundDrawable (new ColorDrawable(Android.Graphics.Color.Transparent));
    
            dlg.Window.SetLayout (ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
        }
    
        public void SetMessage(string msg)
        {
            textView.Text = msg;
        }
    
        public void ShowLoading()
        {
            dlg.Show ();
        }
    
        public void HideLoading()
        {
            dlg.Cancel ();
            textView.Text = "";
        }
    }
    
    Saturday, June 18, 2016 10:21 AM

Answers

  • User171101 posted

    The issue was that the Context changed.

    So I moved the dialog creation into the ShowLoading:

        public void ShowLoading()
        {
            if (isLoading) return;
            isLoading = true;
    
            dlg = new Dialog(Forms.Context);
    
                        ...
    
            dlg.Show();
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, February 23, 2017 12:08 PM

All replies

  • User171101 posted

    Ok i've finally been able to reproduce this error.

    When you 'close' the app and resume it, thats when the activity changes from time to time.

    And then the Dialog Service still points to the 'old' activity.

    Now to find a solution.

    Saturday, June 18, 2016 1:22 PM
  • User101855 posted

    @ReinV Have you resolved that issue?

    Thursday, February 23, 2017 11:33 AM
  • User171101 posted

    The issue was that the Context changed.

    So I moved the dialog creation into the ShowLoading:

        public void ShowLoading()
        {
            if (isLoading) return;
            isLoading = true;
    
            dlg = new Dialog(Forms.Context);
    
                        ...
    
            dlg.Show();
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, February 23, 2017 12:08 PM