[这是我的MainActivity.java文件] [1]
package com.example.socialmediaintegration;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Arrays;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import de.hdodenhof.circleimageview.CircleImageView;
公共类MainActivity扩展了AppCompatActivity {
private LoginButton loginButton;
private CircleImageView circleImageView;
private TextView txtName, txtEmail;
private CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// private static final String EMAIL = "email";
loginButton = findViewById(R.id.login_button);
circleImageView = findViewById(R.id.profile_pic);
txtName = findViewById(R.id.profile_name);
txtEmail = findViewById(R.id.profile_email);
callbackManager = CallbackManager.Factory.create();
loginButton.setPermissions(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(MainActivity.this, "User Logged out", Toast.LENGTH_LONG).show();
}
else {
loadUserProfile(currentAccessToken);
}
}
};
private void loadUserProfile(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 = "http://graph.facebook.com/"+id+"/picture?type=normal";
txtEmail.setText(email);
txtName.setText(first_name + " " + last_name);
RequestOptions requestOptions = new RequestOptions();
requestOptions.dontAnimate();
Glide.with( MainActivity.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)
{
loadUserProfile(AccessToken.getCurrentAccessToken());
}
}
}
这是我在Logcat中显示的错误->>
2020-10-12 12:01:23.621 19597-19602 / com.example.socialmediaintegration I / zygote:将代码缓存容量增加到256KB 2020-10-12 12:01:27.373 19597-19623 / com.example.socialmediaintegration E / GraphResponse:{HttpStatus:400,errorCode:100,subErrorCode:33,errorType:GraphMethodException,errorMessage:不支持的获取请求。 ID为'980066249173751'的对象不存在,由于缺少权限而无法加载,或者不支持此操作。请阅读https://developers.facebook.com/docs/graph-api}上的Graph API文档 2020-10-12 12:01:32.329 19597-19623 / com.example.socialmediaintegration E / GraphResponse:{HttpStatus:400,errorCode:100,subErrorCode:-1,errorType:OAuthException,errorMessage:(#100)尝试访问不存在节点类型(用户)上的字段(email_id)} 2020-10-12 12:01:32.329 19597-19597 / com.example.socialmediaintegration D / AndroidRuntime:关闭VM 2020-10-12 12:01:32.332 19597-19597 / com.example.socialmediaintegration E / AndroidRuntime:FATAL EXCEPTION:main 流程:com.example.socialmediaintegration,PID:19597 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String org.json.JSONObject.getString(java.lang.String)' 在com.example.socialmediaintegration.MainActivity $ 3.onCompleted(MainActivity.java:102) 在com.facebook.GraphRequest $ 1.onCompleted(GraphRequest.java:317) 在com.facebook.GraphRequest $ 5.run(GraphRequest.java:1398) 在android.os.Handler.handleCallback(Handler.java:790) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6626) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
答案 0 :(得分:0)
在此错误行
Attempt to invoke virtual method 'java.lang.String
org.json.JSONObject.getString(java.lang.String)' on a null object reference
据说您的JSONObject实例为null,并抛出NullPointerException
从JSONObject读取数据之前,您首先需要检查请求是否成功
“检查处理错误”部分