[英]Android Facebook sdk GraphRequest returns null when get user info
[英]Android: Profile info is NULL for first time logging in using Facebook SDK 4.1
當第一次記錄配置文件為空時,之后它可以正常工作。 我是否正確使用Profile Tracker和AccessToken Tracker? 我沒有使用Profile.getcurrentprofile(),因為我被告知它不起作用。 新錯誤:當我退出Facebook並嘗試通過按Facebook登錄按鈕登錄我的Android應用程序時,登錄屏幕會打開但我收到錯誤消息:
java.lang.NullPointerException:嘗試在空對象引用上調用虛方法'void com.facebook.AccessTokenTracker.stopTracking()'
這個問題與這些問題類似,但解決方案不起作用,並且沒有提供解決方案。
Profile.getCurrentProfile()在登錄后返回null(FB API v4.0)
Facebook SDK:為什么Profile.getCurrentProfile()總是第一次返回null?
我的更新代碼如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_main, container, false);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
// If using in a fragment
loginButton.setFragment(this);
// Other app specific specialization
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
// Set the access token using
// currentAccessToken when it's loaded or set.
Profile.fetchProfileForCurrentAccessToken();
AccessToken.setCurrentAccessToken(currentAccessToken);
}
};
accessTokenTracker.startTracking();
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
// App code
if(currentProfile!=null)
{
Profile.setCurrentProfile(currentProfile);
profile = currentProfile;
}
}
};
profileTracker.startTracking();
// App code
//token you have been granted to access the facebook sever
AccessToken accessToken = loginResult.getAccessToken();
//user's profile thats login
profile = Profile.getCurrentProfile();
final Bundle extras = new Bundle();
//error is HERE PROFILE IS NULL
extras.putString(EXTRA_PROFILENAME, profile.getFirstName());
extras.putString(EXTRA_PROFILEID, profile.getId());
.... rest code
}
}
@Override
public void onDestroy() {
super.onDestroy();
// accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
public void onStop(){
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
您需要在您的onSuccess()
方法中放置ProfileTokenTracker
和AccessTokenTracker
並在那里啟動它,然后在onDestroy()
或onStop()
停止跟蹤。 這樣你就可以在登錄時從用戶個人資料中獲取數據。這是我的onSuccess()
方法:
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken accessToken, AccessToken accessToken1) {
}
};
accessTokenTracker.startTracking();
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile profile, Profile profile1) {
}
};
profileTracker.startTracking();
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
//get data here
}
}
試着移動
accessTokenTracker.startTracking();
profileTracker.startTracking();
Profile.fetchProfileForCurrentAccessToken();
進入onSuccess()方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.