简体   繁体   English

Android:首次使用Facebook SDK 4.1登录时,个人资料信息为NULL

[英]Android: Profile info is NULL for first time logging in using Facebook SDK 4.1

When logging the first time the profile is null and after that it works. 当第一次记录配置文件为空时,之后它可以正常工作。 Am I using the Profile Tracker and AccessToken Tracker correctly? 我是否正确使用Profile Tracker和AccessToken Tracker? I am not using Profile.getcurrentprofile() as I was told it will not work. 我没有使用Profile.getcurrentprofile(),因为我被告知它不起作用。 NEW ERROR: When I am logged out of Facebook and I try to login to my android app by pressing the Facebook Login Button the login screen opens but I receive an error: 新错误:当我退出Facebook并尝试通过按Facebook登录按钮登录我的Android应用程序时,登录屏幕会打开但我收到错误消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.AccessTokenTracker.stopTracking()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void com.facebook.AccessTokenTracker.stopTracking()'

This question is similar to these questions but the solution does not work and there is no solution provided. 这个问题与这些问题类似,但解决方案不起作用,并且没有提供解决方案。

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

Facebook SDK: Why does Profile.getCurrentProfile() always return null for the first time? Facebook SDK:为什么Profile.getCurrentProfile()总是第一次返回null?

My UPDATED code is below: 我的更新代码如下:

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

You need to put ProfileTokenTracker and AccessTokenTracker in your onSuccess() method and start it there and then stop tracking in onDestroy() or onStop() . 您需要在您的onSuccess()方法中放置ProfileTokenTrackerAccessTokenTracker并在那里启动它,然后在onDestroy()onStop()停止跟踪。 That way you start getting data from users profile when he is logged in. This is my onSuccess() method: 这样你就可以在登录时从用户个人资料中获取数据。这是我的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
        }
}

Try to move 试着移动

accessTokenTracker.startTracking();
profileTracker.startTracking();
Profile.fetchProfileForCurrentAccessToken();

into onSuccess() method. 进入onSuccess()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM