2016-08-18 144 views
1

我想在登录Facebook后获取用户信息。安卓Facebook登录后会获取用户信息

callbackManager = CallbackManager.Factory.create(); 
    loginButton = (LoginButton) findViewById(R.id.login_button); 
    loginButton.setReadPermissions("user_birthday", "email"); 
    final Context contextM = this.getApplicationContext(); 


    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      String firstName = Profile.getCurrentProfile().getFirstName(); 
      String lastName = Profile.getCurrentProfile().getLastName(); 
      String url = Profile.getCurrentProfile().getProfilePictureUri(100, 100).toString(); 

      final ImageView mImageView = (ImageView) findViewById(R.id.imageView); 
      ImageRequest request = new ImageRequest(url, 
        new Response.Listener<Bitmap>() { 
         @Override 
         public void onResponse(Bitmap bitmap) { 
          mImageView.setImageBitmap(bitmap); 
         } 
        }, 0, 0, null, 
        new Response.ErrorListener() { 
         public void onErrorResponse(VolleyError error) { 
         } 
        }); 
      MySingleton.getInstance(contextM).addToRequestQueue(request); 
     } 

,这是错误我得到:

显示java.lang.NullPointerException:试图调用虚拟方法 'java.lang.String中com.facebook.Profile.getFirstName()' 上null 对象引用

回答

0

我已经使用图请求访问了如下结果。检查是否可以帮助你

LoginManager.getInstance().registerCallback(callbackManager, 
     new FacebookCallback<LoginResult>(){ 
     @Override 
     public void onSuccess(final LoginResult loginResult){ 
       GraphRequest request=GraphRequest.newMeRequest(
       loginResult.getAccessToken(), 
       new GraphRequest.GraphJSONObjectCallback(){ 
     @Override 
     public void onCompleted(
       JSONObject object, 
       GraphResponse response){ 
       if(response.getError()!=null){ 
       // handle error 
       }else{ 
       //**access details as follows** 
       String email=object.optString("email"); 
       String first_name=object.optString("first_name"); 
       String last_name=object.optString("last_name"); 

       } 
       }); 

       Bundle parameters=new Bundle(); 
       parameters.putString("fields","id,first_name,last_name,email,gender,birthday"); 
       request.setParameters(parameters); 
       request.executeAsync(); 
       } 

     @Override 
     public void onCancel(){ 
       //Toast.makeText(context, "Something went wrong. Please try again", 
       // Toast.LENGTH_SHORT).show(); 
       } 

     @Override 
     public void onError(FacebookException exception){ 
       //Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show(); 
       exception.printStackTrace(); 
       } 
}); 
+0

@Yura Abramov是你的问题解决了吗? – Nikhil

+0

是的,谢谢你们! –

0

看起来你已经错过了public_profile权限,它允许你同时获得first_name和lastname。

只需使用getRecentlyGrantedPermissions()检查您授予的所有权限。

+0

你不需要任何额外的权限来获得first_name和last_name,只是基本的授权。绝对不是public_profile。只需在api explorer中试试看:/ me?fields = first_name,last_name – luschn

相关问题