Facebook onCompleted()中的NullPointerException

时间:2015-07-08 15:09:34

标签: java android facebook

我的应用程序崩溃了,我无法在我的任何设备上复制。

我的Crashlytics返回三个问号???并给出如下的堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
   at myproject.app.android.SignLog.SignLogFragment$2.onCompleted()
   at com.facebook.Request$1.onCompleted()
   at com.facebook.Request$4.run()
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6141)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

它说空指针异常是由在null对象上调用toString()引起的,但是这一切都是在try catch块中调用的,所以我很困惑这会导致即使存在空值也会崩溃。

                @Override
                public void onCompleted(GraphUser user, Response response) {

                    if (response.getError() == null && user != null)
                    {
                        try {
                            params.put("first_name", user.getFirstName());
                            params.put("last_name", user.getLastName());
                            params.put("email_address", user.getProperty("email").toString());
                            params.put("user_gender", user.getProperty("gender").toString());

                            String image = "http://graph.facebook.com/" + user.getId() + "/picture?type=large";
                            try {
                                byte[] data = image.getBytes("utf-8");
                                image = Base64.encodeToString(data, Base64.DEFAULT);
                                image = image.replaceAll("(?:\\r\\n|\\n\\r|\\n|\\r)", "");
                            } catch (Exception e) {
                            }
                            params.put("image_url", image);
                            params.put("social_access_token", session.getAccessToken());
                            params.put("social_user_identifier", user.getId());
                            params.put("social_login_source", "Facebook");

                            NetworkTaskHandler nHandler = new NetworkTaskHandler(getActivity(), true);

                            String url = "social_login";
                            nHandler.callAsyncTask(params, url, "Logging in...", true, new AsyncTaskCompleteListener() {

                                @Override
                                public void onAsyncTaskComplete(String response, boolean success) {

                                    if (success == true) {
                                        setUpUserDetails(response);
                                    } else {
                                        Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
                                        callFacebookLogout();
                                    }
                                }


                            });
                        }catch(Exception e)
                        {
                            Toast.makeText(getActivity(), "Could not log in", Toast.LENGTH_SHORT).show();
                            revokeFacebookPermissions(session);
                            callFacebookLogout();
                        }
                    }
                    else
                    {
                        Toast.makeText(getActivity(), "Could not log in", Toast.LENGTH_SHORT).show();
                        revokeFacebookPermissions(session);
                        callFacebookLogout();
                    }
                }

正如我上面所说,我实际上无法复制此次崩溃,但我的一些应用用户正在发生这种情况,主要是HTC one(m8)和三星Galaxy S6。

1 个答案:

答案 0 :(得分:1)

检查属性emailgender

params.put("email_address", user.getProperty("email") != null ? user.getProperty("email").toString() : "");
params.put("user_gender", user.getProperty("gender") != null ? user.getProperty("gender").toString() : "");

<强>已更新