在Facebook上共享时获取空指针异常

时间:2014-01-22 05:30:32

标签: android facebook

我正在使用FaceBook分享的以下代码。图片发布在一些FaceBook ID中但是对于某些FaceBook ID我得到空指针异常。如何我解决了这个问题。

   postParams.putString("message", message);
   postParams.putString("picture",picture);
   System.out.println("******* 111111 *********");

   com.facebook.Request.Callback callback = new  com.facebook.Request.Callback()
    {
     public void onCompleted(Response response)
      {
         JSONObject graphResponse =response.getGraphObject().getInnerJSONObject();
         String postId = null;
         try {
         postId = graphResponse.getString("id");
         } catch (JSONException e) 
         {
         Log.i("postthis", "JSON error " + e.getMessage());
         }
         FacebookRequestError error = response.getError();
         if (error != null) {
            Log.i("postthis", "JSON error "     +error.getErrorMessage());
            } 
         else {
//       Toast.makeText(ctx.getApplicationContext(), postId,
//                       Toast.LENGTH_LONG).show();
          }
          }
          };

这是logcat错误:

01-22 09:43:20.812: E/AndroidRuntime(5201): FATAL EXCEPTION: main
01-22 09:43:20.812: E/AndroidRuntime(5201): java.lang.NullPointerException
01-22 09:43:20.812: E/AndroidRuntime(5201):     at FBLogin$2.onCompleted  (FBLogin.java:316)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at com.facebook.Request$4.run(Request.java:1669)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at android.os.Handler.handleCallback(Handler.java:615)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at android.os.Looper.loop(Looper.java:137)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at android.app.ActivityThread.main(ActivityThread.java:4895)
01-22 09:43:20.843: E/android.os.Debug(353): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

3 个答案:

答案 0 :(得分:1)

检查你的参数,如果任何一个参数为null,则图形对象在facebook共享中显示空值。

答案 1 :(得分:0)

在墙上发布的会话为空,因此存在空指针异常,您必须首先激活您的会话才能在墙上发布。您可以使用以下代码在墙上共享。

   Session.openActiveSession(activity, true, new Session.StatusCallback() {

    // callback when session changes state
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        if (session != null && session.isOpened()) {

            // Check for publish permissions
            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                        activity, PERMISSIONS);
                session.requestNewPublishPermissions(newPermissionsRequest);
                return;
            }

            Bundle postParams = new Bundle();
            postParams.putString("message", message);

                postParams.putString("tags",tag);
                     postParams.putString("place",place_id);



            Request.Callback callback = new Request.Callback() {
                private String toastmessage;

                public void onCompleted(Response response) {
                    try {
                        JSONObject graphResponse = response
                                .getGraphObject().getInnerJSONObject();
                        String postId = null;

                        postId = graphResponse.getString("id");
                    } catch (Exception e) {
                        Log.i("Test", "JSON error " + e.getMessage());
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        isPosted(false);
                        Toast.makeText(
                                activity.getApplicationContext(),
                                error.getErrorMessage(),
                                Toast.LENGTH_SHORT).show();
                    } else {
                        isPosted(true);
                        toastmessage = "Posted Successfully";
                        Toast.makeText(activity, toastmessage,
                                Toast.LENGTH_SHORT).show();
                        {

                        }
                    }
                }
            };

            Request request = new Request(session, "me/feed",
                    postParams, HttpMethod.POST, callback);

            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();
        }


    }
});

在此链接How do I publish a check in to facebook with android SDK

答案 2 :(得分:0)

尝试此操作后,这对我Android sdk 3.x

有用
 void publishToWall(Session session) {      
    if (session != null)  {          
        Bundle postParams = new Bundle();
        postParams.putString("message", "contentStr");
        postParams.putString("description",  "description");
        postParams.putString("link", "");
        postParams.putString("name", "Name");
        postParams.putString("picture", "picture url");

        Request.Callback callback = new Request.Callback()  {
            public void onCompleted(Response response)  {       

            }
        };
        Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();             

        Log.i("TAG", "Message posted to your facebook wall!..");         


    }           
 }