gpt4 book ai didi

android - GraphRequest 在尝试获取用户 onCompleted 的 ID 时抛出 NullPointerException?

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:58 26 4
gpt4 key购买 nike

我几乎 95% 的用户都顺利通过了登录过程,但有时我在尝试时遇到此异常:editor.putString("fbId", id)。为什么?如果 onSuccess 被调用,我假设该对象已经被填充,那么为什么会出现错误或可能的空响应?

请帮帮我,我快疯了!

谢谢!

异常(exception):

Android Version: android version: 4.3

java.lang.NullPointerException at com.cerculdivelor.SplashActivity$2$1.onCompleted(SplashActivity.java:90) at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304) at com.facebook.GraphRequest$5.run(GraphRequest.java:1379) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) at dalvik.system.NativeStart.main(Native Method)

方法:

private void onFblogin() {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList(
"public_profile", "email"));

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());

// Application code
try {

/*** line 90 -->***/ String id = object.getString("id");
String name = object.getString("name");
String email = object.getString("email");
//String birthday = object.getString("birthday"); <-- error if people don't have this completed

Log.i("TAG", email + " " + id + " " + " " + name);

SharedPreferences.Editor editor = getSharedPreferences("user", MODE_PRIVATE).edit();
editor.putString("fbId", id);
editor.putString("name", name);
editor.putString("email", email);
editor.commit();

HttpConActivity http = new HttpConActivity(getApplication());
http.saveToServer(id, name, email);

finish();

} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();


}

@Override
public void onCancel() {
Log.i("TAG", "Canceled!");
}

@Override
public void onError(FacebookException e) {
Log.i("TAG", "Error!");
}
});

最佳答案

只需检查 GraphJSONObjectCallback - GraphResponse 中的错误。文档说,JSONObject 对象 可以为空,因此您不应假设它不为空。

请记住,您是在与服务器通信。您应该始终预料到错误并加以处理。

所以像这样:

@Override
public void onCompleted(JSONObject object, GraphResponse response) {

FacebookRequestError error = response.getError();
if (error != null) {
// handle your error
return;
}

try {
String id = object.getString("id");
...

关于android - GraphRequest 在尝试获取用户 onCompleted 的 ID 时抛出 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37476001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com