Facebook Android:java.lang.UnsupportedOperationException:Session:尝试请求new has pending request

时间:2014-12-05 12:04:06

标签: android facebook facebook-graph-api android-facebook facebook-sdk-3.14.x

我在我的应用程序中发布到Facebook粉丝页面,但我收到错误,我无法弄明白。

错误是:

  

java.lang.UnsupportedOperationException:Session:尝试了   为具有待处理请求的会话请求新权限。

我可以在我的墙上发布数据,但是当我想在我的应用程序中的Facebook粉丝页面上发布数据时,我收到了上述错误。
代码片段是:

public class FacebookShareFragment extends Fragment {

    private ProgressDialog progressDialog;
    private UiLifecycleHelper uiHelper;
    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions","manage_pages","publish_stream");
    private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
    private boolean pendingPublishReauthorization = false;

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

 @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.facebook_share_fragment, container, false);
        if (savedInstanceState != null) {
            pendingPublishReauthorization =
                    savedInstanceState.getBoolean(PENDING_PUBLISH_KEY, false);
        }
 shareOnFanPage();
 return view;
}

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
    }
@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);
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            if (pendingPublishReauthorization &&
                    state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
                pendingPublishReauthorization = false;
       //         publishStory();
                shareOnFanPage();
            }
        } else if (state.isClosed()) {
        }
    }

    /**
     *  Method to publish on the fan page
     */
    public void shareOnFanPage(){
        Session session = Session.getActiveSession();
        if (session != null) {
            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                pendingPublishReauthorization = true;
                Session.NewPermissionsRequest newPermissionsRequest = new Session
                        .NewPermissionsRequest(this, PERMISSIONS);
                session.requestNewPublishPermissions(newPermissionsRequest); // AT THIS POINT I AM GETTING THE ERROR.
                return;
            }

            // Show a progress dialog because the batch request could take a while.
            progressDialog = ProgressDialog.show(getActivity(), "",
                    getActivity().getResources().getString(R.string.progress_dialog_text), true);

            try {
                final RequestBatch requestBatch = new RequestBatch();
                requestBatch.add(fbFan());
                requestBatch.executeAsync();
            }catch(Exception e){
                    e.printStackTrace();
            }
        }
    }

首先我能够在My FB墙上发布数据但是后来根据要求我添加了新的权限“manage_pages”&amp; “publish_stream”,但不知道为什么我收到错误,即使我评论了在我的FB墙上发布数据的publishStory()方法。

我阅读了很多文章但却无法弄明白...

1 个答案:

答案 0 :(得分:0)

我相信在onActivityResult之前调用onCreateView。在这种情况下,如果您的主机活动在权限请求完成之前被终止(或者如果您打开了“不要保持活动”开发选项),那么将始终在onActivityResult之前调用此方法。

由于你也在你的onCreateView中调用了shareOnFanPage,你基本上会在前一个请求完成之前发出另一个权限请求(因为当你将它传递给uiLifecycleHelper时它已经在onActivityResult中完成了)。

我会将shareOnFanPage()调用移动到onResume,或者只是完全删除它,因为我认为你不需要它,因为你已经打算在你的onResume方法中调用onSessionStateChanged。