NullPointer试图在没有对话的情况下在Facebook上发布

时间:2015-02-23 12:15:53

标签: android facebook facebook-graph-api publish facebook-sdk-3.0

我正在尝试使用facebook sdk发布到用户墙中,使用以下代码打开图表:

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;
}

我们从以下代码获取代码:on publish feed, facebook 3.0 examples

登录不会抛出任何错误,但是当我们尝试发布它什么都不做的东西时,它会从facebook日志中返回一个空指针异常。

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

我们已经检查了keyhash,它似乎是正确的,我们有点绝望。任何帮助将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:2)

确保:

1。您已使用OpenSSL正确生成了app-id并在应用程序清单中声明了它。

2。应用程序包名称和应用程序ID已在您的Facebook应用程序详细信息页面上正确输入。

3。您已申请相关权限并已从FB收到。

4。访问令牌有效(即非空)。

5. 访问令牌的会话状态为OPENED。