【发布时间】:2020-10-11 17:55:27
【问题描述】:
[这是我的 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: 致命例外: 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)
【问题讨论】:
-
NullPointerException
-
你能帮我修一下吗!
-
请在您的问题中添加一些代码,之后我们可能能够获取确切的错误。
标签: android-studio crash logcat