android-studio - 每次我运行它时,android应用程序都会意外崩溃
问题描述
[这是我的 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:致命异常:主进程:com.example.socialmediaintegration,PID:19597 java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String org.json.JSONObject.getString( java.lang.String)' 在 com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:317) 的 com.example.socialmediaintegration.MainActivity$3.onCompleted(MainActivity.java:102) 的空对象引用上。facebook.GraphRequest$5.run(GraphRequest.java:1398) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper。在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller 的 java.lang.reflect.Method.invoke(Native Method) 的 android.app.ActivityThread.main(ActivityThread.java:6626) 的循环(Looper.java:164)。在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 运行(RuntimeInit.java:438)第438章)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)第438章)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
解决方案
在这个错误行
Attempt to invoke virtual method 'java.lang.String
org.json.JSONObject.getString(java.lang.String)' on a null object reference
据说你的 JSONObject 实例为 null 并抛出 NullPointerException
在从 JSONObject 读取数据之前,您需要首先检查请求是否成功
检查处理错误部分
推荐阅读
- ios - 在 UITableView 中存储 UICollectionView 的选定 indexPath
- php - 将关联数组映射到另一个数组php
- c - 为什么 XServer 发送 FocusOut 通知两次
- mysql - 使用 MySQL 使用最新插入的值检索多行
- css - 在不换行的情况下使用徽章内的菜单。来自 react-bootstrap 框架
- bash - -bash: cd: /tensorflow1: ubuntu 中没有这样的文件或目录,但是文件在那里
- azure-devops - 在史诗可以关闭之前强制关闭子任务的规则
- ubuntu - Ubuntu 18.04:在域帐户下运行服务
- methods - Groovy multi-methods
- libgdx - LibGDX ModelInstance - can't reset scale, setting the transform while scaled deforms the model