1

我想要發佈到用戶的牆,使用Facebook的SDK開放圖形與下面的代碼:on publish feed, facebook 3.0 examples空指針試圖在Facebook上發佈不對話框

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions"); 
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization"; 
private boolean pendingPublishReauthorization = false; 
private UiLifecycleHelper uiHelper; 
private Button shareButton; 

    private Session.StatusCallback callback = new Session.StatusCallback() { 
@Override 
public void call(Session session, SessionState state, 
     Exception exception) { 
    onSessionStateChange(session, state, exception); 
} 
}; 
protected String TAG; 

@Override 
public void onResume() { 
super.onResume(); 
Session session = Session.getActiveSession(); 
if (session != null && (session.isOpened() || session.isClosed())) { 
    onSessionStateChange(session, session.getState(), null); 
} 
uiHelper.onResume(); 
} 

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

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

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

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


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
uiHelper = new UiLifecycleHelper(getActivity(), callback); 
uiHelper.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
View view = inflater.inflate(R.layout.activity_main, container, false); 
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton); 
shareButton = (Button) view.findViewById(R.id.shareButton); 
shareButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     publishStory();   
    } 
}); 
authButton.setFragment(this); 



return view; 
} 

private void onSessionStateChange(Session session, SessionState state,Exception exception) { 
if (state.isOpened()) { 
    shareButton.setVisibility(View.VISIBLE); 
    if (pendingPublishReauthorization && 
      state.equals(SessionState.OPENED_TOKEN_UPDATED)) { 
     pendingPublishReauthorization = false; 
     publishStory(); 
    } 

} else if (state.isClosed()) { 
    shareButton.setVisibility(View.INVISIBLE); 
} 
} 


private void publishStory() { 
Session session = Session.getActiveSession(); 
Log.i(TAG,session.toString()); 
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("name", "Facebook SDK for Android"); 
    postParams.putString("caption", "Build great social apps and get more installs."); 
    postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps."); 
    postParams.putString("link", "https://developers.facebook.com/android"); 
    postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png"); 

    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(getActivity() 
        .getApplicationContext(), 
        error.getErrorMessage(), 
        Toast.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(getActivity() 
         .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; 
} 

從我們得到的代碼

登錄不會拋出任何錯誤,但是當我們嘗試發佈一些它什麼都不做的時候,並且從facebook日誌中返回一個空指針異常。

02-23 11:46:54.389: D/com.facebook.AppEventsLogger(5652): Caught unexpected exception while flushing: java.lang.NullPointerException 

我們檢查了keyhash,它似乎是正確的,我們有點絕望。任何幫助將不勝感激。

謝謝。

回答

2

確保:

您是否正確產生的APP-ID與OpenSSL和在應用程序清單中聲明它。

2.應用程序包名稱和應用程序ID已正確輸入您的Facebook應用程序詳細信息頁面。

3.您已申請相關權限並從FB處獲得。

4.訪問令牌有效(即非空)。

5.訪問令牌的會話狀態是OPENED。

+0

感謝您的支持者,我們認爲我無所不能。我們不確定的唯一一點是第四點,我們正在檢查 – Tofasio 2015-02-23 12:48:24

+0

@Tofasio:你能找到錯誤嗎? – 2015-02-26 14:33:13

+0

對不起,延遲,我們嘗試了其他發佈方法,最終奏效了。我會將你的答案標記爲有效的:) – Tofasio 2015-02-26 15:09:57