Facebook Android SDK v2.3意外调用LoginManager.onActivityResult [英] Facebook Android SDK v2.3 Unexpected call to LoginManager.onActivityResult

查看:111
本文介绍了Facebook Android SDK v2.3意外调用LoginManager.onActivityResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<<编辑>>
我正在尝试在我的Android应用程序中升级facebook SDK.我正在按照Facebook开发人员网站上的文档进行操作.我想稍作修改

<< EDITED >>
I'm trying to upgrade the facebook SDK in my android app. I'm doing it following the docs on the facebook developers site. I want to make to slight modifications

1)我想从DialogFragment而不是直接在活动中进行登录
2)我想使用自己的按钮登录Facebook

1) I want to make the login from a DialogFragment not directly in the activity
2) I want to do the Facebook login using my own button

这是我的活动的样子:

public class MainActivity extends ActionBarActivity {
    private static final String TAG = MainActivity.class.getName();

    private CallbackManager mFBCallbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Setup FB SDK add login callback
        FacebookSdk.sdkInitialize(this.getApplicationContext());
        mFBCallbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(mFBCallbackManager, new FacebookCallback<LoginResult>() {

            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "FB Login successfull");
            }

            @Override
            public void onCancel() {
                Log.d(TAG, "FB Login cancelled");
            }

            @Override
            public void onError(FacebookException e) {
                Log.d(TAG, "Error while performing FB login", e);
            }
        });

        // Open login dialog
        Button loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new LoginDialog().show(getSupportFragmentManager(), TAG);
            }
        });
    }

    @Override
    protected void onActivityResult( int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // foward onActivityResult call to the FB callback manager
        mFBCallbackManager.onActivityResult(requestCode, requestCode, data);
    }
}

这是我的DialogFragment的样子:

And this is what my DialogFragment looks like:

public class LoginDialog extends DialogFragment {

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View rootView = inflater.inflate(R.layout.login_dialog, null);

        Button fbLoginBtn = (Button) rootView.findViewById(R.id.fb_login_btn);
        fbLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginManager.getInstance().logInWithReadPermissions(
                        getActivity(),
                        Arrays.asList("basic_info"));
            }
        });

        builder.setView(rootView);
        return builder.create();
    }
}

我认为设置看起来像Facebook所提到的必需步骤,但是我叫FacebookCallback.onError(FacebookException),要求在logcat上显示此设置. 关于异常消息对LoginManager.onActivityResult的意外调用"的特定消息

I think the setup looks like what the Facebook mentions as required steps however I'm my FacebookCallback.onError(FacebookException) is getting called showing this on the logcat. Specific message on the exception "Unexpected call to LoginManager.onActivityResult"

04-03 12:01:26.390    8229-8229/com.jpdevs.fbv23 D/com.jpdevs.fbv23.MainActivity﹕ Error while performing FB login
    Unexpected call to LoginManager.onActivityResult
            at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:187)
            at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:140)
            at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
            at com.jpdevs.fbv23.MainActivity.onActivityResult(MainActivity.java:65)
            at android.app.Activity.dispatchActivityResult(Activity.java:6135)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
            at android.app.ActivityThread.access$1300(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

如果您想了解更多详细信息,我将使用整个项目创建此仓库: https://github.com/jorgep19/fb_login

If you would like to more details I created this repo with the whole project: https://github.com/jorgep19/fb_login

推荐答案

我遇到了这个问题,当我告诉您秘密"时,您将进行面部护理. 如果您只想解决问题,我将解释为什么加薪.

I had this problem and you will do a facepalm when i tell you the "secret". I will explain why this raise, if you just want the solution go to the end.

facebook的源代码在发生这种情况时抛出此错误:

The source code of facebook throw this error when this happens:

if (exception == null && newToken == null && !isCanceled) {
    exception = new FacebookException("Unexpected call to LoginManager.onActivityResult");
}

这3个参数正在发生某些事情.

So something is happening with this 3 params.

我正在阅读代码,我发现resultCode可能存在一些问题,因为它没有返回Activity.RESULT_OK.

I was reading the code and i see that could be some problem with the resultCode because it's not returning an Activity.RESULT_OK.

:您在resultCode参数上也两次传递了requestCode… 只需为此更改:

THE KEY: You have passed the requestCode twice, on the resultCode param too... Just change this for this:

mFBCallbackManager.onActivityResult(requestCode, requestCode, data);

作者

mFBCallbackManager.onActivityResult(requestCode, resultCode, data);

这篇关于Facebook Android SDK v2.3意外调用LoginManager.onActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆