如何在Android中获取Facebook用户信息?

时间:2016-03-20 21:06:13

标签: android facebook facebook-graph-api

我正在使用此代码在我的应用中访问用户信息。但没有任何改变..准确当我调试代码时,login.registerCallback及以下从未调用过。没有错误,没有任何改变。

我的错在哪里?

   @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(getApplicationContext());
            callbackManager = CallbackManager.Factory.create();
            setContentView(R.layout.activity_main);

            loginButton = (LoginButton) findViewById(R.id.login_button);
            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Profile profile = Profile.getCurrentProfile();
                    String userID = loginResult.getAccessToken().getUserId();
                    String profileImgUrl = "https://graph.facebook.com/" + userID + "/picture?type=large";
                    tv = (TextView) findViewById(R.id.textView);
                    tv.setText(profile.getName());

                    try {
                        bitmap = getBitmapFromUrl(profileImgUrl);
                        iv = (ImageView) findViewById(R.id.imageView);
                        iv.setImageBitmap(bitmap);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

1 个答案:

答案 0 :(得分:2)

它是因为您忘记在活动中添加以下块:

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