开发者

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

开发者 https://www.devze.com 2023-03-02 21:07 出处:网络
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).

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).

I have a 'publish' button, and its job is to show the dialog if the user is logged in to his FB account, or s开发者_如何学Pythonhow the login dialog(and then immediately show the 'post on wall' dialog) if the user isn't logged in to his account.

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 -

public void onAuthSucceed() {
    postOnWall();
}

Here's the PostOnWall function(which shows the publish dialog) -

 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. I'm working on it for 2 days now, and couldn't find anything about it.

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. 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

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. Probably in the postOnWall-method. Is the this in that method the currently displayed activity?


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. 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消