getCurrentProfile返回null(Facebook SDK)

时间:2016-06-24 09:22:54

标签: android facebook

我在facebook sdk应用程序上工作,我对配置文件错误有疑问。当我想用profile.getName()填充文本字段时,profile始终为null。

@Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = AccessToken.getCurrentAccessToken();
            Profile profile = Profile.getCurrentProfile();

            if(profile != null){
                mTextDetails.setText("Welcome " + profile.getName() );
            }

        }

2 个答案:

答案 0 :(得分:0)

这一个

    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject user, GraphResponse response) {
                        Log.d(TAG, "Response: " + user)
                    }
                });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,first_name,last_name,link,gender,birthday,email");
    request.setParameters(parameters);
    request.executeAsync();

答案 1 :(得分:0)

Profile是异步加载的,所以它在登录后立即返回null。

试试这个

@Override
public void onSuccess(LoginResult loginResult) {
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    Profile profile = Profile.getCurrentProfile();

    if(profile != null){
        mTextDetails.setText("Welcome " + profile.getName() );
    } else {        
        ProfileTracker profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                this.stopTracking();
                Profile.setCurrentProfile(currentProfile);      
                mTextDetails.setText("Welcome " + currentProfile.getName() );
            }
        };
        profileTracker.startTracking();
    }
}