Facebook Android SDK 4.0.0没有获取个人资料信息

时间:2015-04-13 10:50:49

标签: android facebook facebook-login facebook-android-sdk

在我的应用中登录Profile.getCurrentProfile()后获取Facebook时遇到问题。我正在使用Facebook Android SDK 4.0.0。登录后,我想获取用户名并在TextView中显示,但我得到了profile == null而且我不知道原因。我有正确的应用ID和HashKey

这是我的代码:

public class MyFragment extends Fragment {

    CallbackManager callbackManager;
    LoginButton loginButton;
    FacebookCallback<LoginResult> loginResultFacebookCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Log.e("FB", String.valueOf(accessToken));
            Profile profile = Profile.getCurrentProfile();

            if (profile != null) {
                name.setText("Witam " + profile.getName());
                Log.e("FB", "w");
            }
        }

        @Override
        public void onCancel() {
            // App code
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
        }
    };
    TextView name;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myfragment_facebook, container, false);
        name = (TextView) view.findViewById(R.id.name);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
        loginButton.setFragment(this);
        loginButton.registerCallback(callbackManager, loginResultFacebookCallback);
    }

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

当我在Logcat中检查令牌时,我得到AccessToken token:ACCESS_TOKEN_REMOVED

5 个答案:

答案 0 :(得分:3)

对我来说,代码Profile profile = Profile.getCurrentProfile();当安装FB android应用程序时,同样会抛出空指针异常,当fb app被卸载时,因此在那个实例中我的应用程序称为fb web app(没有返回配置文件)。这可能是FB中的现有错误SDK

答案 1 :(得分:1)

答案 2 :(得分:1)

答案 3 :(得分:1)

而不是loginresult.getAccessToken(),您必须使用loginresult.getAccessToken().getToken()。它返回包含访问令牌的字符串。

答案 4 :(得分:0)

我有同样的问题,这个错误是因为:

  

Profile.getProfile:是异步任务

我使用此代码解决了这个问题:

          LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    if(Profile.getCurrentProfile() == null) {
                        mProfileTracker[0] = new ProfileTracker() {
                            @Override
                            protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
                                Log.v("facebook - profile", profile2.getFirstName());
                                storeLogin(profile2.getName(), profile2.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                                openMainActivity(profile2.getName(), profile2.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                                loadingFace.dismiss();
                                mProfileTracker[0].stopTracking();
                            }
                        };
                        mProfileTracker[0].startTracking();
                    }
                    else {
                        Profile profile = Profile.getCurrentProfile();
                        storeLogin(profile.getName(), profile.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                        openMainActivity(profile.getName(), profile.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                        loadingFace.dismiss();
                    }
                }
                @Override
                public void onCancel() {
                 ....

最好的问候