FB Android:Session:尝试打开已经打开的会话

时间:2014-03-12 11:09:37

标签: android facebook facebook-android-sdk android-simple-facebook

我正在用android-simple-facebook库实现Facebook登录 它在Eclipse示例中运行良好,但在我的JNI应用程序中,它崩溃时出现以下错误:

java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
at com.facebook.Session.open(Session.java:985)                                                   
at com.facebook.Session.openForPublish(Session.java:423)                                         
at com.sromku.simple.fb.SessionManager.openSession(SessionManager.java:237)                      
at com.sromku.simple.fb.SessionManager.reopenSession(SessionManager.java:332)                    
at com.sromku.simple.fb.SessionManager.isLogin(SessionManager.java:123)                          
at com.sromku.simple.fb.SessionManager.login(SessionManager.java:57)                             
at com.sromku.simple.fb.SimpleFacebook.login(SimpleFacebook.java:171)                            
at com.js.helpers.FBHelper$2.run(FBHelper.java:109)                                              
at android.os.Handler.handleCallback(Handler.java:730)                                           
at android.os.Handler.dispatchMessage(Handler.java:92)                                           
at android.os.Looper.loop(Looper.java:137)                                                       
at android.app.ActivityThread.main(ActivityThread.java:5103)                                     
at java.lang.reflect.Method.invokeNative(Native Method)                                          
at java.lang.reflect.Method.invoke(Method.java:525)                                              
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)               
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)                                  
at dalvik.system.NativeStart.main(Native Method)                                                 

我的登录实施非常简单:

    public static void login() {

    final OnLoginListener onLoginListener = new OnLoginListener() {
        @Override
        public void onFail(String reason) {
            Log.w(TAG, "onLoginListener failed: " + reason);
        }

        @Override
        public void onException(Throwable throwable) {
            Log.e(TAG, "OnLoginListener exception: ", throwable);
        }

        @Override
        public void onThinking() {
            // show progress bar or something to the user while login is happening
            Log.i(TAG, "onLoginListener in progress...");
        }

        @Override
        public void onLogin() {
            // change the state of the button or do whatever you want
            Log.i(TAG, "Logged in");
            loginCB();
        }

        @Override
        public void onNotAcceptingPermissions(Permission.Type type) {
            Log.i(TAG, "onLoginListener NotAcceptingPermissions: " + type);
        }
    };

    mSimpleFacebook.login(onLoginListener);
    Log.i(TAG, "FBHelper.login");
}

loginCB 是我的原生回调函数。

问题浮出的最烦人的事情是,应用程序有时可能会登录,但大多数都会崩溃 我会感激任何帮助!

2 个答案:

答案 0 :(得分:1)

您的onActivityResult

似乎错过了Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data); 
    super.onActivityResult(requestCode, resultCode, data);
} 

让我知道它是否适合你。

答案 1 :(得分:0)

我确定这是JNI问题。以下组合使我的代码工作:

  • 将OnLoginListener向上移动一级
  • 添加 runOnUiThread
  • 添加 try / catch
  • 检查 mSimpleFacebook!= null 并修复 IsLoggedIn 功能,以便在已经登录时不要求登录(好吧,我看到了这种情况在图书馆处理,但Facebook真的试图重复登录。

所以,我的最终代码是:

private static OnLoginListener mOnLoginListener = new OnLoginListener() {
         // the same code as in question above
};

public static void login(final Activity activity) {

    activity.runOnUiThread(new Runnable() {
        public void run() {
            if (mSimpleFacebook == null) 
                mSimpleFacebook = SimpleFacebook.getInstance(activity);
            try {
                mSimpleFacebook.login(mOnLoginListener);
            } catch (Exception ex) {
                Log.e(TAG, "Login error: ", ex);
            }
        }
    });
}