CookieSyncManager :: createInstance()IllegalStateException?

时间:2012-07-23 20:39:39

标签: android facebook-android-sdk

我的应用程序最近开始收到很多CookieSyncManager异常:

java.lang.IllegalStateException: CookieSyncManager::createInstance() 
    needs to be called before CookieSyncManager::getInstance()
at android.webkit.CookieSyncManager.getInstance(CookieSyncManager.java:81)
at android.webkit.CookieManager$2.run(CookieManager.java:532)
at java.lang.Thread.run(Thread.java:1096)

错误从堆栈跟踪中清除,但它的起源不是(这是来自Play商店报告)。附加到堆栈跟踪的用户消息表明它正在我正在使用facebook SDK的地方发生,它确实使用了CookieSyncManager:

// From Facebook.java
private void startDialogAuth(Activity activity, String[] permissions) {
    ...
    CookieSyncManager.createInstance(activity); // <-- here
    dialog(activity, LOGIN, params, new DialogListener() {
        public void onComplete(Bundle values) {
            CookieSyncManager.getInstance().sync(); // <-- here
            ...

https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/android/Facebook.java#L342

这对我来说没问题,但上述用法有什么问题吗?我无法在我的任何测试设备上重现错误,可能存在实现差异?

谢谢

2 个答案:

答案 0 :(得分:1)

应该像异常一样说:

public void onComplete(Bundle values) {
   CookieSyncManager.createInstance( activity );    // <-- this line was missing
   CookieSyncManager.getInstance().sync();
   ...

答案 1 :(得分:0)

根据android文档

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CookieSyncManager.createInstance(MainActivity.this);
    CookieSyncManager.getInstance().startSync();
}
public void onReume(){
    super.onResume();
    CookieSyncManager.getInstance().stopSync();
}
public void onPause(){
    super.onPause();
    CookieSyncManager.getInstance().sync();
}