Profile.getCurrentProfile()在login后返回null(FB API v4.0)

出于某种原因,在使用FB API v4.0login到FaceBook之后, Profile.getCurrentProfile()显示null。

这在我的应用程序中导致我的问题,因为我无法显示我的下一个Profile为空的Activity

我之所以说它是空的原因有时是因为如果我closures了我的应用程序,并重新打开它,我可以去我的下一个Activity ,但如果我还没有login,然后login, Profile为空。 这似乎是一个短暂的时间。

有没有解决这个问题?

就像哈代说的 ,你必须创build一个ProfileTracker的实例,它将开始跟踪configuration文件的更新(即ProfileTracker.onCurrentProfileChanged()将在用户的configuration文件完成提取时调用)。

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

 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()如下:

 @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; } } 

编辑:

代码更新与亚历克斯Zezekalo的build议只能调用mProfileTracker.startTracking(); 如果Profile.getCurrentProfile()返回null。

遵循Sufian的回答,您还可以将新的configuration文件保存到Profile类:

 @Override public void onSuccess(LoginResult loginResult) { ProfileTracker profileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) { this.stopTracking(); Profile.setCurrentProfile(currentProfile); } }; profileTracker.startTracking(); } 

Facebookasynchronous加载configuration文件信息,所以即使从logincallback中获得结果,Profile.getCurrentProfile()也会返回null。

但是,每当用户通过Facebook(第一次和随后的每个时间)login时,他的个人资料将会改变,并将触发个人档案追踪器。 这是你必须调用configuration文件的地方。

这里是你应该如何构build你的代码。 您必须监听ProfileTracker以更新您的用户属性 – 避免在login过程中在跟踪器外面调用getCurrentProfile:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); profileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged(Profile profile, Profile profile1) { //Listen for changes to the profile or for a new profile: update your //user data, and launch the main activity afterwards. If my user has just logged in, //I make sure to update his information before launching the main Activity. Log.i("FB Profile Changed", profile1.getId()); updateUserAndLaunch(LoginActivity.this); } }; profileTracker.startTracking(); accessTokenTracker = new AccessTokenTracker() { @Override protected void onCurrentAccessTokenChanged( AccessToken oldAccessToken, AccessToken currentAccessToken) { //Check that the new token is valid. This tracker is fired //when the user logs in the first time and afterwards any time he interacts with //the Facebook API and there is a change in his permissions. if (!accessTokenIsValid(currentAccessToken)){ Log.i("FB Token Updated", String.valueOf(currentAccessToken.getPermissions())); requestLogin(); } } }; // User already has a valid access token? Then take the user to the main activity if (accessTokenIsValid(AccessToken.getCurrentAccessToken())){ launchApp(); }else{ //Show the Facebook login page requestLogin(); } } 

这里的关键是,你不应该从logincallback(LoginManager,registercallback或者loginButton.registercallback)调用Profile.getcurrentprofile – 值是不可靠的。 设置跟踪器,并依靠它在适当的时间点燃,以获得更新的个人资料信息。