简体   繁体   English

尝试显示 Facebook 对话框时出现“BadTokenException:无法添加窗口” - Android

[英]Getting 'BadTokenException: Unable to add window' when trying to show Facebook dialog - Android

I'm getting a Bad Token Exception when trying to show the publish on wall dialog from the Facebook SDK(It happens every 2 times I run the app).尝试从 Facebook SDK 显示墙上发布对话框时出现错误令牌异常(它每运行 2 次我运行应用程序就会发生一次)。

I have a 'publish' button, and its job is to show the dialog if the user is logged in to his FB account, or show the login dialog(and then immediately show the 'post on wall' dialog) if the user isn't logged in to his account.我有一个“发布”按钮,如果用户登录到他的 FB 帐户,它的工作是显示对话框,或者如果用户登录,则显示登录对话框(然后立即显示“墙上的帖子”对话框) t 登录到他的帐户。

Here's the on click listener of the Publish button -这是发布按钮的点击监听器 -

    mPostButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if(mLoginButton.getVisibility() == 0)
                postOnWall();
            else
                mLoginButton.performClick();
        }
    });  

Here's the onAuthSucceed() listener -这是 onAuthSucceed() 监听器 -

public void onAuthSucceed() {
    postOnWall();
}

Here's the PostOnWall function(which shows the publish dialog) -这是 PostOnWall 函数(显示发布对话框) -

 public void postOnWall()
 {
    Bundle params = new Bundle();
    params.putString("name", FBname);
    params.putString("link", FBlink);
    params.putString("description", FBdescription);
    params.putString("picture", FBpicture);
    con = this;
    mFacebook.dialog(con, "feed", params, new SampleDialogListener());
 }

Here's the log of the error -这是错误的日志 -

 05-05 16:25:09.601: WARN/WindowManager(109): Attempted to add application window with unknown token HistoryRecord{405416b0 android.alco/.do_drive}.  Aborting.
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@405db9f8 is not valid; is your activity running?
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.view.ViewRoot.setView(ViewRoot.java:527)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.app.Dialog.show(Dialog.java:241)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at com.facebook.android.Facebook.dialog(Facebook.java:622)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.alco.do_drive.postOnWall(do_drive.java:258)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.alco.do_drive$SampleAuthListener.onAuthSucceed(do_drive.java:172)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.alco.SessionEvents.onLoginSuccess(SessionEvents.java:78)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.alco.LoginButton$LoginDialogListener.onComplete(LoginButton.java:100)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at com.facebook.android.Facebook$1.onComplete(Facebook.java:308)
 05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at com.facebook.android.FbDialog$FbWebViewClient.shouldOverrideUrlLoading(FbDialog.java:133)

I've looked all over and I can't find a solution.我已经看遍了,我找不到解决方案。

EDIT - This only happens when the user is not logged in. In that case, he presses the 'publish' button, logs in, and then the 'postOnWall' function is called, which generates the error.编辑 - 这仅在用户未登录时发生。在这种情况下,他按下“发布”按钮,登录,然后调用“postOnWall”function,这会产生错误。 I'm working on it for 2 days now, and couldn't find anything about it.我现在正在研究它 2 天,但找不到任何关于它的信息。

Thanks!谢谢!

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
    Log.d("Facebook-WebView", "Webview loading URL: " + url);
    super.onPageStarted(view, url, favicon);
    if(FbDialog.this.isShowing())
        mSpinner.show();
}

And of course there is the case where code such as当然也有这样的代码

View view = getLocalActivityManager().startActivity(id, intent).getDecorView();   
setContentView(view);

is ran inside an ActivityGroup derived class - as in the case of switching between many Activities under a single tab of a tabbed Activity.在 ActivityGroup 派生的 class 内运行 - 就像在选项卡式活动的单个选项卡下的多个活动之间切换的情况一样。 Be sure that the 'this' in确保“这个”在

AlertDialog.Builder builder = new AlertDialog.Builder(WhateverActivity.this);

gets replaced by a proper reference to the WhateverActivity's parent like被替换为对 WhatActivity 父级的正确引用,例如

AlertDialog.Builder builder = new AlertDialog.Builder(ActivityGroupDerivedParentOfWhateverActivity.this);

It looks like you've provided some method with an context/activity object that is no longer active.看起来您提供了一些方法,其中包含不再活动的上下文/活动 object。 Probably in the postOnWall-method.可能在 postOnWall 方法中。 Is the this in that method the currently displayed activity?该方法中的this是当前显示的活动吗?

Facing the same issue.面临同样的问题。 As far as I can tell, from googling, its to do with your activity sometimes not being ready when the Facebook login activity returns its result.据我所知,从谷歌搜索来看,当 Facebook 登录活动返回其结果时,这与您的活动有时尚未准备好有关。 Why?为什么? No clue.没有线索。

But I think this will work better: Define a flag in your class, set the flag in the AuthListener, and show the alert in the onStart if flag is set.但我认为这会更好:在 class 中定义一个标志,在 AuthListener 中设置标志,如果设置了标志,则在 onStart 中显示警报。 Then you will for sure be in the comfort of your own activity:然后,您肯定会在自己的活动中感到舒适:

private boolean authSuccess = false;

... ...

public class SampleAuthListener implements AuthListener {
    authSuccess = true;

... ...

protected void onStart() {
    super.onStart();
    if (authSuccess) {
        alert.show();
    }
}

Disclaimer: I have very little Java/Android experience.免责声明:我的 Java/Android 经验很少。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用getApplicationContext()显示对话框 - BadTokenException:无法添加窗口 - Show dialog using getApplicationContext() - BadTokenException: Unable to add window 获取WindowManager $ BadTokenException:无法添加窗口 - Getting WindowManager$BadTokenException : Unable to add window BadTokenException无法添加窗口 - BadTokenException Unable to add window BadTokenException:无法添加 window - BadTokenException: Unable to add window android.view.WindowManager $ BadTokenException:无法在自定义对话框打开时添加窗口 - android.view.WindowManager$BadTokenException: Unable to add window on custom Dialog opening Android:“BadTokenException:无法添加窗口; 你的活动在运行吗?” 在 PreferenceActivity 中显示对话框 - Android: “BadTokenException: Unable to add window; is your activity running?” at showing dialog in PreferenceActivity 尝试显示Facebook Android SDK添加朋友对话框时出错 - Error when trying to show Facebook Android SDK Add Friend Dialog 进度对话框-WindowManager $ BadTokenException:无法添加窗口-令牌null不适用于应用程序 - Progress Dialog - WindowManager$BadTokenException: Unable to add window — token null is not for an application 在dialog.show()中获取BadTokenException - getting BadTokenException in dialog.show() buider.show() 上的“android.view.WindowManager$BadTokenException:无法添加窗口” - "android.view.WindowManager$BadTokenException: Unable to add window" on buider.show()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM