Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 226
Linjiong Cai Avatar asked Aug 21 '14 23:08

Linjiong Cai


2 Answers

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~

like image 106
Veer Avatar answered Oct 12 '22 07:10

Veer


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();
}
like image 29
Nazmus Saadat Avatar answered Oct 12 '22 07:10

Nazmus Saadat