在尝试获取用户的ID时,GraphRequest抛出NullPointerException?

时间:2016-05-27 06:09:19

标签: android json facebook facebook-graph-api nullpointerexception

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

请帮帮我,我对此感到疯狂!

谢谢!

异常:

  

Android版:android版:4.3

     

java.lang.NullPointerException at   com.cerculdivelor.SplashActivity $ 2 $ 1.onCompleted(SplashActivity.java:90)   在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)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)at   dalvik.system.NativeStart.main(原生方法)

方法:

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!");
            }
        });

1 个答案:

答案 0 :(得分:2)

只需检查client.Search<Project>(s => s .Query(q => q .Match(m => m .Field(f => f.Field.Suffix("subfield")) .Query("the query") ) ) ); 中的错误。 文档说,GraphJSONObjectCallback - GraphResponse可以为null,所以你不应该认为它不是null。

请记住,您与服务器进行通信。 你应该总是期待错误并处理它们。

这样的事情:

JSONObject object