2014-01-15 44 views
0

我的代碼工作正常,但我有問題。當我再次點擊張貼在相同的清單項目獲得武力關閉。如果我點擊另一個列表項目將成功發佈。基本上不能再發布相同的text1沒有發佈其他文本並返回發佈text1。FaceBook:發佈相同的狀態到用戶牆上得到錯誤

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions"); 
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization"; 
private boolean pendingPublishReauthorization = false; 
private static final String TAG = "FaceBook"; 
private Boolean isShared; 

private UiLifecycleHelper uiHelper; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_view); 
    uiHelper = new UiLifecycleHelper(this, callback); 
    uiHelper.onCreate(savedInstanceState); 

     if (savedInstanceState != null) { 
     pendingPublishReauthorization = savedInstanceState.getBoolean(
       PENDING_PUBLISH_KEY, false); 
    } 
    ls = (ListView) findViewById(android.R.id.list); 
    ls.setAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, 
      0, ITEMS)); 


} 

    @Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() { 
     @Override 
     public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) { 
      Log.e("Activity", String.format("Error: %s", error.toString())); 
     } 

     @Override 
     public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) { 
      Log.i("Activity", "Success!"); 
     } 
    }); 
} 

    private class MyAdapter extends ArrayAdapter<String> { 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    public MyAdapter(Context context, int resource, int textViewResourceId, 
      ArrayList<String> iTEMS) { 
     super(context, resource, textViewResourceId, iTEMS); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.view_item, parent, 
        false); 

     } 
     tv_content_item = (TextView) convertView 
       .findViewById(R.id.tv_content); 

     tv_content_item.setText(ITEMS.get(position)); 
     tv_content_item.setTag(position); 
     tv_content_item.setTypeface(Typeface.createFromAsset(getAssets(), 
       "fonts/Hayah.otf")); 

     TextView tv_counter = (TextView) convertView 
       .findViewById(R.id.text_counter); 
     tv_counter.setText(String.valueOf(position + 1)); 

     Button facebook = (Button) convertView 
       .findViewById(R.id.btn_share_face); 

     facebook.setOnClickListener(ViewActivity.this); 
     return convertView; 
    } 

    @Override 
    public String getItem(int position) { 
     // TODO Auto-generated method stub 
     return super.getItem(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return super.getItemId(position); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return ITEMS.size(); 
    } 

} 



@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

    final TextView tv = (TextView) ls.findViewWithTag(ls 
      .getPositionForView(v)); 

    switch (v.getId()) { 

     case R.id.btn_share_face: 


      if(!isShared){ 

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

      // callback when session changes state 
      @Override 
      public void call(Session session, SessionState state, 
        Exception exception) { 
       if (session.isOpened()) { 
        isShared=true; 
         publishStory(tv.getText().toString()); 

       } 
      } 
     }); 

     }else{ 
      Session session = Session.getActiveSession(); 

      if (session.isOpened()) { 
        publishStory(tv.getText().toString()); 


     } 
     } 
     break; 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    uiHelper.onResume(); 

} 



@Override 
public void onPause() { 
    super.onPause(); 
    uiHelper.onPause(); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    uiHelper.onDestroy(); 

} 

private Session.StatusCallback callback = new Session.StatusCallback() { 
    @Override 
    public void call(Session session, SessionState state, Exception exception) { 
     onSessionStateChange(session, state, exception); 
    } 
}; 
private void onSessionStateChange(Session session, SessionState state, Exception exception) { 
    if (state.isOpened()) { 
     Log.i(TAG, "Logged in..."); 
    } else if (state.isClosed()) { 
     Log.i(TAG, "Logged out..."); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization); 
    uiHelper.onSaveInstanceState(outState); 
} 

private void publishStory(String txt) { 
    Session session = Session.getActiveSession(); 

    if (session != null){ 

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

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

     Request.Callback callback= new 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(TAG, 
         "JSON error "+ e.getMessage()); 
       } 
       FacebookRequestError error = response.getError(); 
       if (error != null) { 
        Toast.makeText(ViewActivity.this 
         .getApplicationContext(), 
         error.getErrorMessage(), 
         Toast.LENGTH_SHORT).show(); 
        } else { 
         Toast.makeText(ViewActivity.this 
          .getApplicationContext(), 
          postId, 
          Toast.LENGTH_LONG).show(); 
       } 
      } 
     }; 

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

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

} 
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) { 
    for (String string : subset) { 
     if (!superset.contains(string)) { 
      return false; 
     } 
    } 
    return true; 
} 

我logcat的

01-15 07:10:29.771: W/dalvikvm(4584): threadid=1: thread exiting with uncaught exception (group=0x41e8c898) 
01-15 07:10:29.771: E/AndroidRuntime(4584): FATAL EXCEPTION: main 
01-15 07:10:29.771: E/AndroidRuntime(4584): java.lang.NullPointerException 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at com.droidibuilders.aqual_alolma.ViewActivity$4.onCompleted(ViewActivity.java:352) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at com.facebook.Request$4.run(Request.java:1669) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at android.os.Handler.handleCallback(Handler.java:730) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at android.os.Looper.loop(Looper.java:137) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at android.app.ActivityThread.main(ActivityThread.java:5450) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at java.lang.reflect.Method.invoke(Method.java:525) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
01-15 07:10:29.771: E/AndroidRuntime(4584):  at dalvik.system.NativeStart.main(Native Method) 
+0

發佈您的logcat連同行號 –

回答

1

你的代碼是罰款沒有與任何代碼的問題。唯一的是facebook SDK不允許發佈multiple status with the same text。我想一分鐘的時間表。在1minute之後,您可以多次發佈相同的狀態。如果你改變文字可以說試試append the current time with your post text,這樣兩個狀態永遠不會相同。你現在可以多次發佈相同的狀態,但時間會有所不同。

+0

雖然我不確定時間線。 – 2014-01-15 05:13:44

+0

如果我編輯我的代碼由此>>> Bundle postParams = new Bundle(); \t postParams.putString(「message」,txt); \t postParams.putString(「name」,getString(R.string.app_name)); \t postParams.putString(「link」,「https://play.google.com/store/apps/details?id=com.droidibuilders.aqual_alolma」); 問題將會消失..爲什麼? –

+0

抱歉沒有得到你,你想說什麼? – 2014-01-15 05:26:05

0

謝謝..我編輯了我的onComplete並正常工作。

public void onCompleted(Response response) { 
       FacebookRequestError error = response.getError(); 
       if (error != null) { 
        Toast.makeText(ViewActivity.this 
         .getApplicationContext(), 
         getString(R.string.post_faild), 
         Toast.LENGTH_SHORT).show(); 
        } else { 
         Toast.makeText(ViewActivity.this 
          .getApplicationContext(), 
          getString(R.string.post_succeed), 
          Toast.LENGTH_LONG).show(); 
       } 
      } 
     };