我有一个从AysncTask扩展的类。在doInBackground
我想更新cookies的方法中,我有:
CookieSyncManager.createInstance(context); // <<<<<<<<<<<<<<<<<<
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(cookie.getDomain(), cookieString);
CookieSyncManager.getInstance().sync();
但我java.lang.NullPointerException
排在第一线。
所以,问题是:
- 为什么会造成这种情况?
- 我需要在另一个活动中创建它,然后在这个活动中使用它吗?如何 ?为什么 ?
备注:
- 我知道我的上下文不为空,我检查了一下。
- 我不能在AsyncTask上使用onCreate方法。去做这个。
这是堆栈跟踪:
java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
at android.webkit.JniUtil.setContext(JniUtil.java:53)
at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:89)
at com.t.m.library.HttpRequest.doInBackground(HttpRequest.java:129)
at com.t.m.library.HttpRequest.doInBackground(HttpRequest.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
这是我的doInBackground方法:
request = new HttpGet(this.url);
request.addHeader("Accept", "application/json");
request.addHeader("Authorization", this.basicAuthentication);
HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpClient httpclient = null;
httpclient = new DefaultHttpClient(httpParams);
HttpResponse response = httpclient.execute(request);
// The content from the requested URL along with headers, etc.
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity, "UTF-8");
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(cookie.getDomain(), cookieString);
CookieSyncManager.getInstance().sync();