无法添加窗口 - 令牌android.os.BinderProxy无效;你的活动在运行吗?

时间:2014-08-21 23:01:46

标签: android progressdialog

我尝试使用twitter4j连接到Twitter。但它显示了标题所说的错误。

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();
}

这是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)

错误行为if(mProgressDialog != null) mProgressDialog.show();

我尝试了引入Unable to add window — token android.os.BinderProxy is not valid; is your activity running?的方式。但它对于我的Android版本来说太旧了。

3 个答案:

答案 0 :(得分:2)

我犯了与你今天相同的错误,在你要显示对话之前,写下面的代码:

export BUILD_ID=dontKillMe

它对我有用。祝你好运〜

答案 1 :(得分:1)

使用swtAllertDialogBox犯了同样的错误。这段代码可以帮助我避免崩溃

if(!((Activity) context ).isFinishing())
{
    swtDialogSucces();
}else{
    Toast.makeText(getApplicationContext(),"Successful", Toast.LENGTH_LONG).show();
}

答案 2 :(得分:0)

使用此条件。

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

我可以想到一种更简单的方法,但一种方法是自己实施。在onResume()上,您将成员变量mIsRunning = true并将onPause()设置为mIsRunning = false。使用此布尔值,您应该知道不要在回调上调用alert.show()。

希望这会对你有所帮助。