我是这里的新手,因此需要您的帮助。当我尝试运行Facebook登录时出现此错误。我该怎么办?
private LoginButton loginButton;
private CircleImageView circleImageView;
private TextView txtName, txtEmail;
private CallbackManager callbackManager;
public void loginToFacebook(View view){
Intent intent = new Intent(this,FacebookLogin.class);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebook_login);
loginButton = findViewById(R.id.login_button);
txtName = findViewById(R.id.profile_name);
txtEmail = findViewById(R.id.profile_email);
circleImageView = findViewById(R.id.profile_pic);
callbackManager = CallbackManager.Factory.create();
loginButton.setReadPermissions(Arrays.asList("email","public_profile"));
checkLoginStatus();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
callbackManager.onActivityResult(requestCode,resultCode,data);
super.onActivityResult(requestCode, resultCode, data);
}
AccessTokenTracker tokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
if(currentAccessToken==null)
{
txtName.setText("");
txtEmail.setText("");
circleImageView.setImageResource(0);
Toast.makeText(FacebookLogin.this, "User Has Logged Out Successfully.", Toast.LENGTH_LONG).show();
}
else
LoadUsersProfile(currentAccessToken);
}
};
private void LoadUsersProfile (AccessToken newAccessToken)
{
GraphRequest request = GraphRequest.newMeRequest(newAccessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String first_name = object.getString("first_name");
String last_name = object.getString("last_name");
String email = object.getString("email");
String id = object.getString("id");
String image_url = "https://graph.facebook.com/"+id+ "/picture?type=normal";
txtEmail.setText(email);
txtName.setText(first_name + " " + last_name);
RequestOptions requestOptions = new RequestOptions();
requestOptions.dontAnimate();
Glide.with(FacebookLogin.this).load(image_url).into(circleImageView);
}
catch (JSONException e){
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields","first_name,last_name,email.id" );
request.setParameters(parameters);
request.executeAsync();
}
private void checkLoginStatus(){
if (AccessToken.getCurrentAccessToken() !=null)
{
LoadUsersProfile(AccessToken.getCurrentAccessToken());
}
}
}
错误:
4230-4230 / com.example.musicstream E / Android运行时:致命异常:主要 流程:com.example.musicstream,PID:4230 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String org.json.JSONObject.getString(java.lang.String)' at com.example.musicstream.FacebookLogin $ 3.onCompleted(FacebookLogin.java:114) 在com.facebook.GraphRequest $ 1.onCompleted(GraphRequest.java:316) 在com.facebook.GraphRequest $ 5.run(GraphRequest.java:1395) 在android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6669) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)