2013-07-31 40 views
3

我想在webview onOverrideUrlLoading()方法中獲取cookie,並且出現此錯誤。請各位看看下面我的代碼,java.lang.IllegalStateException:需要在CookieSyncManager :: getInstance()之前調用CookieSyncManager :: createInstance()

WebViewClient loginClient = new WebViewClient() { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      CookieManager cookieManager = CookieManager.getInstance(); 
      final String cookie = cookieManager.getCookie(url); 

      //Some Code after this 
     } 
} 

而我會收到此錯誤,

java.lang.IllegalStateException: CookieSyncManager::createInstance() needs to be called before CookieSyncManager::getInstance() 

回答

1

使用CookieSyncManager.createInstance(this);在活動的onCreate()方法。在致電getInstance()之前,錯誤清楚地說明createInstance()需要被調用。

+1

我也看到了同樣的錯誤。我在onCreate方法中調用了CookieSyncManager.createInstance(this),在onResume中調用了CookieSyncManager.getInstance()。startSync()和onPause中的CookieSyncManager.getInstance()。stopSync()。我仍然有時會看到相同的錯誤 – aasha

相關問題