没有从Android API上的Facebook API获取任何个人资料信息

时间:2016-04-24 15:33:21

标签: android facebook android-fragments textview

我正在尝试为Android学习Facebook API。我试图登录并记录它。但我无法从用户个人资料中获取任何信息。它不会在textview中返回任何旨在显示登录的Facebook用户名称的内容。

public class ProfileScreenFragment extends Fragment {

    private TextView mTextView;

    private AccessTokenTracker mTokenTracker;
    private ProfileTracker mProfileTracker;

    private CallbackManager mCallbackManager;
    private FacebookCallback<LoginResult> mCallback=new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken=loginResult.getAccessToken();
            Profile profile=Profile.getCurrentProfile();
            displayWelcomeMessage(profile);
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException error) {

        }
    };

    public ProfileScreenFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

        mCallbackManager=CallbackManager.Factory.create();

        mTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

            }
        };

        mProfileTracker=new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {

            }
        };
        mTokenTracker.startTracking();
        mProfileTracker.startTracking();


    }
    public void displayWelcomeMessage(Profile profile){
        if(profile!=null){
            mTextView.setText("Welcome "+profile.getName());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_profile_screen, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LoginButton loginButton=(LoginButton)view.findViewById(R.id.login_button);

        loginButton.setReadPermissions("public_profile");

        loginButton.setFragment(this);

        loginButton.registerCallback(mCallbackManager,mCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCallbackManager.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    public void onResume() {
        super.onResume();
        Profile profile = Profile.getCurrentProfile();
        displayWelcomeMessage(profile);
    }

    @Override
    public void onStop() {
        super.onStop();
        mTokenTracker.stopTracking();
        mProfileTracker.stopTracking();
    }
}

这是我在logcat中得到的:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getName()' on a null object reference

如何使用displayWelcomeMessage()函数中的textview获取配置文件名称?感谢。

1 个答案:

答案 0 :(得分:0)

尝试将权限更改为数组列表,如下所示:

loginButton.setReadPermissions(Arrays.asList("public_profile"));

请参阅https://developers.facebook.com/docs/facebook-login/android/permissions以获取更多信息。