当第一次登录时,配置文件为空,在此之后它工作。 我正确使用Profile Tracker和AccessToken Tracker吗? 我没有使用Profile.getcurrentprofile(),因为我被告知它不起作用。新错误:当我退出Facebook并尝试通过按Facebook登录按钮登录我的Android应用程序时,登录屏幕打开,但我收到一个错误:Android:首次使用Facebook SDK 4.1登录时,配置文件信息为NULL
java.lang.NullPointerException:试图调用虚拟方法'无效com.facebook.AccessTokenTracker.stopTracking()'空对象引用
此问题与这些问题类似,但解决方案不起作用,并且没有提供任何解决方案。
Profile.getCurrentProfile() returns null after logging in (FB API v4.0)
Facebook SDK: Why does Profile.getCurrentProfile() always return null for the first time?
我更新后的代码如下:
@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();
}
Hi @ laky.brat我将两个方法都移动到了onsuccess(),并且除了删除停止跟踪并将其放置在onDestroy()和onStop()之外,其实现方式与之前相同。跟踪器应该像你一样空白吗?我在onStop() – akalanta
上的空对象引用上收到此错误'void com.facebook.AccessTokenTracker.stopTracking()',因为我正在使用profile = Profile.getCurrentProfile();,因此发生此错误。当我注销Facebook应用程序并尝试登录我的应用程序时,会发生这种情况。我按登录按钮打开Facebook登录屏幕,当我得到错误。那么,我应该如何处理AccessTokenTracker? – akalanta
@akalanta请看看这个图片[链接](http://community.monogame.net/uploads/default/820/fcaf25c811f85f46.jpg),然后找出什么时候应该停止'AccessTokenTracker'。最佳做法是使用'onStop()'方法来阻止它,因为您可以克隆应用程序并稍后再回来,然后用户数据将被保存。你可以使用'SharedPrefferences'来存储数据,当没有互联网用户可以看到他的照片,名字等时。 – jelic98