0

我正在使用 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是我的原生回调函数。

最烦人的问题是浮动,应用程序有时可能会登录,但大多是崩溃。
我将不胜感激任何帮助!

4

2 回答 2

1

看起来你错过了:onActivityResult在你的Activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data); 
    super.onActivityResult(requestCode, resultCode, data);
} 

让我知道它是否对您有用。

于 2014-03-12T11:58:52.763 回答
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);
            }
        }
    });
}
于 2014-03-13T13:46:54.063 回答