Profile.getCurrentProfile()在登录后返回null(FB API v4.0) [英] Profile.getCurrentProfile() returns null after logging in (FB API v4.0)

查看:412
本文介绍了Profile.getCurrentProfile()在登录后返回null(FB API v4.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因 Profile.getCurrentProfile()在使用FB API v4.0登录FaceBook后,会显示为空。

For some reason Profile.getCurrentProfile() shows up null at times right after logging into FaceBook, using FB API v4.0.

这在我的应用程序中导致我的问题,因为我无法显示我的下一个活动个人资料为null。

This is causing problems for me in my app because I can't display my next Activity with Profile being null.

有时我说这是null的原因是因为如果我关闭了我的应用程序并重新打开它,我可以得到我的下一个活动,但如果我还没有登录,然后登录,个人资料为null。

The reason I say it's null sometimes is because if I close out of my app and re-open it, I'm able to get to my next Activity, but if I'm not already logged in, and then login, Profile is null. It seems it is for a short period.

有没有办法解决这个问题?

Is there a work around or fix to this?

推荐答案

喜欢 Hardy说,你必须创建一个 ProfileTracker 的实例,它将开始跟踪配置文件更新(即 ProfileTracker.onCurrentProfileChanged ()将在用户的个人资料完成提取时被调用)。

Like Hardy said, you have to create an instance of ProfileTracker which will start tracking profile updates, (i.e ProfileTracker.onCurrentProfileChanged() will be called when the user's profile finishes being fetched).

以下是您需要登录FB的完整代码并获取用户的个人资料:

Following is the complete code that you'd need to login to FB and get the user's profile:

LoginButton loginButton = (LoginButton) findViewById(R.id.btn_facebook);
loginButton.setReadPermissions("public_profile");
mCallbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {

    private ProfileTracker mProfileTracker;

    @Override
    public void onSuccess(LoginResult loginResult) {
        if(Profile.getCurrentProfile() == null) {
            mProfileTracker = new ProfileTracker() {
                @Override
                protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                    Log.v("facebook - profile", currentProfile.getFirstName());
                    mProfileTracker.stopTracking();
                }
            };
            // no need to call startTracking() on mProfileTracker
            // because it is called by its constructor, internally.
        }
        else {
            Profile profile = Profile.getCurrentProfile();
            Log.v("facebook - profile", profile.getFirstName());
        }
    }

    @Override
    public void onCancel() {
        Log.v("facebook - onCancel", "cancelled");
    }

    @Override
    public void onError(FacebookException e) {
        Log.v("facebook - onError", e.getMessage());
    }
});

您必须重写活动或片段的 onActivityResult()如下:

You must override your Activity's or Fragment's onActivityResult() as below:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if you don't add following block,
    // your registered `FacebookCallback` won't be called
    if (mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
        return;
    }
}



编辑:



代码已更新为 mProfileTracker.startTracking(); 如果 Profile.getCurrentProfile() >返回null。

Code updated with Alex Zezekalo's suggestion to only call mProfileTracker.startTracking(); if Profile.getCurrentProfile() returns null.

这篇关于Profile.getCurrentProfile()在登录后返回null(FB API v4.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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