Unable to add window - token android.os.BinderProxy is not valid; is your activity running?
Asked Answered
E

3

6

I try to connect to Twitter using twitter4j. But it shows the error as title said.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    twitterConsumerKey = getIntent().getStringExtra(TWITTER_CONSUMER_KEY);
    twitterConsumerSecret = getIntent().getStringExtra(TWITTER_CONSUMER_SECRET);
    if(twitterConsumerKey == null || twitterConsumerSecret == null){
        Log.e(Constants.TAG, "ERROR: Consumer Key and Consumer Secret required!");
        Login.this.setResult(TWITTER_LOGIN_RESULT_CODE_FAILURE);
        Login.this.finish();
    }

    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setMessage("Please wait...");
    mProgressDialog.setCancelable(false);
    mProgressDialog.setCanceledOnTouchOutside(false);
    mProgressDialog.show();

    twitterLoginWebView = (WebView)findViewById(R.id.twitter_login_web_view);
    twitterLoginWebView.setWebViewClient( new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            if( url.contains(Constants.TWITTER_CALLBACK_URL))
            {
                Uri uri = Uri.parse(url);
                Login.this.saveAccessTokenAndFinish(uri);
                return true;
            }
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            if(mProgressDialog != null) mProgressDialog.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);

            if(mProgressDialog != null) mProgressDialog.show();
        }
    });

    Log.d(Constants.TAG, "ASK OAUTH");
    askOAuth();
}

Here is the LogCat:

FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41f93f30 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:559)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:281)
at com.moviecollection.Login$1.onPageStarted(Login.java:93)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:305)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5295)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)

The error line is if(mProgressDialog != null) mProgressDialog.show();

I tried the way as Unable to add window — token android.os.BinderProxy is not valid; is your activity running? introduced. But it is too old for my Android version.

Enate answered 21/8, 2014 at 23:1 Comment(1)
Please post the logcat and indicate which line causes the error.Untune
C
4

I made the same mistake as you today,before you are gonna show dialog,write this code below:

    if(((Activity) context).isFinishing())
    {
       return;
    }

it works for me. Good luck~

Cincture answered 11/5, 2016 at 10:49 Comment(0)
T
1

It made the same mistake for using swtAllertDialogBox. This code helps me to avoid crash

if(!((Activity) context ).isFinishing())
{
    swtDialogSucces();
}else{
    Toast.makeText(getApplicationContext(),"Successful", Toast.LENGTH_LONG).show();
}
Tavern answered 6/8, 2017 at 6:38 Comment(0)
I
0

Use this condition.

if(mProgressDialog != null && mProgressDialog.isShowing()) 
 mProgressDialog.show();

There may be an easier way I can't think of but one way is to implement it yourself. On onResume() you set a member variable mIsRunning = true and on onPause() back to mIsRunning = false. Using this boolean you should know not to call alert.show() on your callback.

Hope this will help you.

Impanation answered 26/10, 2015 at 7:21 Comment(1)
it won't solve his problem. He need to check if the activity is running or notHorrocks

© 2022 - 2024 — McMap. All rights reserved.