使用简单的Facebook API进行日志记录时出现异常(没有打开会话时请求会话)

时间:2014-12-23 08:18:42

标签: java android facebook session

在尝试将Facebook与Android集成时,我遇到了一些问题。我跟着Tutorial。这是我的代码:

public class EventDetail extends Fragment {
    private UiLifecycleHelper uiHelper;

    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");

    private static String message = "Sample status posted from android app";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), statusCallback);
    uiHelper.onCreate(savedInstanceState);

    ivFacebookShare = (ImageView) eventDetail.findViewById(R.id.ivFacebookShare);

    ivFacebookShare.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            postStatusMessage();
        }
    });

    return eventDetail;
}

private Session.StatusCallback statusCallback = new Session.StatusCallback() {
    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...");
    }
}

public void postStatusMessage() {
    if (checkPermissions()) {
        Request request = Request.newStatusUpdateRequest(
                Session.getActiveSession(), message,
                new Request.Callback() {
                    public void onCompleted(Response response) {
                        if (response.getError() == null)
                            Toast.makeText(getActivity(),
                                    "Status updated successfully",
                                    Toast.LENGTH_LONG).show();
                    }
                });
        request.executeAsync();
    } else {
        requestPermissions();
    }
}

public boolean checkPermissions() {
    Session s = Session.getActiveSession();
    if (s != null) {
        return s.getPermissions().contains("publish_actions");
    } else
        return false;
}

public void requestPermissions() {
    Session s = Session.getActiveSession();
    if (s != null)
        s.requestNewPublishPermissions(new Session.NewPermissionsRequest(
                this, PERMISSIONS));
}

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

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

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

@Override
public void onSaveInstanceState(Bundle savedState) {
    super.onSaveInstanceState(savedState);
    uiHelper.onSaveInstanceState(savedState);
}

}

但是,我收到错误消息为stacktrace:

12-23 16:14:36.907: D/AndroidRuntime(22942): Shutting down VM
12-23 16:14:36.907: W/dalvikvm(22942): threadid=1: thread exiting with uncaught exception (group=0x40c561f8)
12-23 16:14:36.961: E/AndroidRuntime(22942): FATAL EXCEPTION: main
12-23 16:14:36.961: E/AndroidRuntime(22942): java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that is not currently open.
12-23 16:14:36.961: E/AndroidRuntime(22942):    at com.facebook.Session.requestNewPermissions(Session.java:1257)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at com.facebook.Session.requestNewPublishPermissions(Session.java:595)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at nyp.edu.eneighbourhood.EventDetail.requestPermissions(EventDetail.java:280)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at nyp.edu.eneighbourhood.EventDetail.postStatusMessage(EventDetail.java:265)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at nyp.edu.eneighbourhood.EventDetail$4.onClick(EventDetail.java:185)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at android.view.View.performClick(View.java:3620)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at android.view.View$PerformClick.run(View.java:14292)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at android.os.Handler.handleCallback(Handler.java:605)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at android.os.Looper.loop(Looper.java:137)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at android.app.ActivityThread.main(ActivityThread.java:4512)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at java.lang.reflect.Method.invokeNative(Native Method)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at java.lang.reflect.Method.invoke(Method.java:511)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
12-23 16:14:36.961: E/AndroidRuntime(22942):    at dalvik.system.NativeStart.main(Native Method)
12-23 16:14:44.594: I/Process(22942): Sending signal. PID: 22942 SIG: 9

任何指南?提前谢谢。

1 个答案:

答案 0 :(得分:0)

答案正是错误说明的内容,您尚未在会话尚未打开时请求新权限。

您是否在任何地方调用Session.getActiveSession()。open *(其中open *是任何打开的方法)?

在您的代码中,您只检查session!= null,但不检查s.isOpened()。您只能在打开的会话中请求更多权限。