我正在尝试在使用 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: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getFirstName()' on a null object reference
最佳答案
我使用图形请求访问了如下结果。检查这是否对您有帮助
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();
}
});
关于java - Android facebook登录后获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39019524/