问题描述
我有一个活动,显示一个列表视图。在项目单击第二个活动开始它运行的基础上被点击哪个项目获取远程URL的AsyncTask的,分析的结果,然后显示这些结果到另一个列表视图。而异步任务时我有,只是显示图像和文本装载的看法。现在的问题是,如果我开始运行异步任务,那么preSS回来,开始说活动的另一个实例活动,我得到一个强制关闭。
I have an Activity that shows a listview. On item click a second activity is started which runs an AsyncTask that fetches a remote URL based on which item is clicked, parses the results, then displays those results into another list view. While the async task is running I have a "loading" view that just displays an image and text. The problem is if I initiate the activity that runs the async task, then press back and start another instance of said activity, I get a force close.
我已经试过
private class getlist extends AsyncTask implements OnDismissListener{
...
@Override
public void onDismiss(DialogInterface arg0) {
this.cancel(true);
}
}
但问题仍然存在。
我也把
if(!isCancelled()){
setContentView(R.layout.list);
...
etc.
}
在我的onPostExecute电话,但要么这不是preventing从该视图切换,或者问题是另一回事。
into my onPostExecute call, but either this is not preventing the view from being switched, or the problem is something else.
logcat中显示意外的简历时,如果已经恢复......得到的RemoteException发送SETACTIVE(假)的通知然后,位在日志中进一步回落,java.net.SocketTimeoutException:套接字未连接
logcat shows "unexpected resume while already resumed .. got RemoteException sending setActive(false) notification"Then a bit further down in the log, "java.net.SocketTimeoutException: Socket is not connected"
我该如何解决此问题?
更新:
E/AndroidRuntime(19379): Uncaught handler: thread AsyncTask #2 exiting due to uncaught exception
E/AndroidRuntime(19379): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(19379): at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(19379): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
E/AndroidRuntime(19379): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
E/AndroidRuntime(19379): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
E/AndroidRuntime(19379): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
E/AndroidRuntime(19379): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
E/AndroidRuntime(19379): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
E/AndroidRuntime(19379): at java.lang.Thread.run(Thread.java:1096)
E/AndroidRuntime(19379): Caused by: java.lang.NullPointerException
E/AndroidRuntime(19379): at com.metatroid.android.swim.ThreadViewer.getPosts(ThreadViewer.java:153)
E/AndroidRuntime(19379): at com.metatroid.android.swim.ThreadViewer$getlist.doInBackground(ThreadViewer.java:70)
E/AndroidRuntime(19379): at com.metatroid.android.swim.ThreadViewer$getlist.doInBackground(ThreadViewer.java:1)
E/AndroidRuntime(19379): at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(19379): at java.util.concurrent.FutureTask$Sync.innerRun
推荐答案
的实际解决这个问题的方法是使用ThreadSafeClientConnManager用于ClientConnectionManager
The actual solution to this problem was to use ThreadSafeClientConnManager for the ClientConnectionManager
例如。
BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
这篇关于安卓:AsyncTask的过程中处理后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!