我正在使用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 是我的本机回调函数。
问题是浮动的最令人讨厌的事情,应用程序有时可能登录,但大部分是崩溃的。
我感谢任何帮助!
看起来您错过了:Activity
中的onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
让我知道它是否对您有用。
我确定这是一个JNI问题。以下组合使我的代码正常工作:
- 移动OnloginListener一个级别上升
- 添加 runonuithread
- 添加 try/catch block
- 检查 msimplefacebook!= null 和fixing 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);
}
}
});
}