Facebook登录崩溃了java.lang.UnsupportedOperationException

时间:2013-09-12 14:35:24

标签: java android facebook android-facebook

我正在尝试将Facebook登录集成到我的应用中。 用户登录Facebook,然后应用程序获取用户的电子邮件地址,该地址将传递到我的应用程序登录系统(帐户存储在远程mysql数据库中)。 我有以下代码:

public class FbLoginActivity extends Activity {

    private static List<String> permissions;
    Session.StatusCallback statusCallback = new SessionStatusCallback();
    ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fblogin);
        Button fbButton = (Button) findViewById(R.id.fbshare);
        /***** FB Permissions *****/
        permissions = new ArrayList<String>();
        permissions.add("email");
        /***** End FB Permissions *****/
        fbButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Check if there is any Active Session, otherwise Open New
                // Session
                Session session = Session.getActiveSession();
                if (!session.isOpened()) {
                    session.openForRead(new Session.OpenRequest(
                            FbLoginActivity.this).setCallback(statusCallback)
                            .setPermissions(permissions));
                } else {
                    Session.openActiveSession(FbLoginActivity.this, true,
                            statusCallback);
                }
            }
        });
        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, statusCallback,
                        savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            session.addCallback(statusCallback);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                session.openForRead(new Session.OpenRequest(this).setCallback(
                        statusCallback).setPermissions(permissions));
            }
        }
    }

    private class SessionStatusCallback implements Session.StatusCallback {

        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            // Check if Session is Opened or not
            processSessionStatus(session, state, exception);
        }
    }

    public void processSessionStatus(Session session, SessionState state,
            Exception exception) {
        if (session != null && session.isOpened()) {
            if (session.getPermissions().contains("email")) {
                // Show Progress Dialog
                dialog = new ProgressDialog(FbLoginActivity.this);
                dialog.setMessage("Logging in..");
                dialog.show();
                Request.executeMeRequestAsync(session,
                        new Request.GraphUserCallback() {

                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {

                                if (dialog != null && dialog.isShowing()) {
                                    dialog.dismiss();
                                }
                                if (user != null) {
                                    Map<String, Object> responseMap = new HashMap<String, Object>();
                                    GraphObject graphObject = response
                                            .getGraphObject();
                                    responseMap = graphObject.asMap();
                                    Log.i("FbLogin", "Response Map KeySet - "
                                            + responseMap.keySet());
                                    // TODO : Get Email
                                    // responseMap.get("email");
                                    String fb_id = user.getId();
                                    String email = null;
                                    String name = (String) responseMap
                                            .get("name");
                                    if (responseMap.get("email") != null) {
                                        email = responseMap.get("email")
                                                .toString();
                                        Intent i = new Intent(FbLoginActivity.this, FbLogin2Activity.class);
                                        i.putExtra("Email", email);
                                        startActivity(i);
                                    } else {
                                        // Clear all session info & ask user to
                                        // login again
                                        Session session = Session
                                                .getActiveSession();
                                        if (session != null) {
                                            session.closeAndClearTokenInformation();
                                        }
                                    }
                                }
                            }
                        });
            } else {
                session.requestNewReadPermissions(new Session.NewPermissionsRequest(
                        FbLoginActivity.this, permissions));
            }
        }
    }

    /********** Activity Methods **********/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("FbLogin", "Result Code is - " + resultCode + "");
        Session.getActiveSession().onActivityResult(FbLoginActivity.this,
                requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Save current session
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }

    @Override
    protected void onStart() {
        // TODO Add status callback
        super.onStart();
        Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    protected void onStop() {
        // TODO Remove callback
        super.onStop();
        Session.getActiveSession().removeCallback(statusCallback);
    }
}  

然后我得到以下错误

09-12 15:28:07.728: E/AndroidRuntime(30897): FATAL EXCEPTION: main
09-12 15:28:07.728: E/AndroidRuntime(30897): java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.facebook.Session.open(Session.java:947)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.facebook.Session.openForRead(Session.java:385)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.test.app.FbLoginActivity$1.onClick(FbLoginActivity.java:48)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.view.View.performClick(View.java:4278)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.view.View$PerformClick.run(View.java:17430)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.os.Handler.handleCallback(Handler.java:725)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.os.Looper.loop(Looper.java:213)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.app.ActivityThread.main(ActivityThread.java:5092)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at java.lang.reflect.Method.invoke(Method.java:511)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at dalvik.system.NativeStart.main(Native Method)

但有时它在某些设备上运行正常。

2 个答案:

答案 0 :(得分:1)

在你的fbButton的onClick监听器中,你有:

Session session = Session.getActiveSession();
if (!session.isOpened()) {
    session.openForRead(...);
} else {
    Session.openActiveSession(...);
}

但是在你的else子句中,会话已经打开,所以你不需要再打开它。相反,你应该做的是:

Session session = Session.getActiveSession();
if (session == null) {
    Session.openActiveSession(...);
} else if (!session.isOpened()) {
    session.openForRead(...);
}

答案 1 :(得分:0)

它说有人试图打开一个已经打开的会话,所以你试图重新打开一个已经打开的会话。它可能与缓存的会话有关。你可以看看这个链接: Session.getActiveSession() returns null when starting the app, although I'm connected