[英]Async Task progress bar in for loop
- 我试图使用异步任务方法来访问Web服务。
- 这里for循环将从第一个中删除进度对话框并使应用程序崩溃。 请检查此代码并提供建议。
码
for(Pageview pg:views)
{
value1=url1+"userid="+pg.getuserid()+"&chapterid="+pg.getchapterid()+"&pageno="+pg.getpageno()+"&view%20time="+pg.getviewtime()+"&IMEI%20no="+pg.getimeino()+"&feedback="+pg.getfeedback()+"&Comments="+pg.getcomments();
//Toast.makeText(getApplicationContext(), value1, Toast.LENGTH_LONG).show();
System.out.println(value1);
// feedbackdata feedback1=new feedbackdata();
feedbak.execute(value1);
feedbackdata.loadingProgress = GUIStaticMethod.returnProgressBar(getApplicationContext());
//myLoginDataFatcher.start();
GUIStaticMethod.mProgressDialog.setOnDismissListener(new OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
if(res1.contains("Page View Insertion Successfully")==true)
{
Toast.makeText(getApplicationContext(), res1, Toast.LENGTH_LONG).show();
}
else if(res1.contains("Page View Insertion Failed")==true)
{
}
}
});
错误
04-02 10:52:34.956: E/AndroidRuntime(234): Uncaught handler: thread main exiting due to uncaught exception
04-02 10:52:34.976: E/AndroidRuntime(234): java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
04-02 10:52:34.976: E/AndroidRuntime(234): at android.os.AsyncTask.execute(AsyncTask.java:383)
04-02 10:52:34.976: E/AndroidRuntime(234): at com.example.wireframe.webviewurl$11.onDismiss(webviewurl.java:1751)
04-02 10:52:34.976: E/AndroidRuntime(234): at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1058)
04-02 10:52:34.976: E/AndroidRuntime(234): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 10:52:34.976: E/AndroidRuntime(234): at android.os.Looper.loop(Looper.java:123)
04-02 10:52:34.976: E/AndroidRuntime(234): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-02 10:52:34.976: E/AndroidRuntime(234): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 10:52:34.976: E/AndroidRuntime(234): at java.lang.reflect.Method.invoke(Method.java:521)
04-02 10:52:34.976: E/AndroidRuntime(234): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-02 10:52:34.976: E/AndroidRuntime(234): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-02 10:52:34.976: E/AndroidRuntime(234): at dalvik.system.NativeStart.main(Native Method)
为每次迭代创建一个新的AsyncTask
实例。
new AsyncTask().execute(value);
原因是您无法再次执行线程,因为一旦线程使用run()
方法完成,您就无法重新启动它。 这就是为什么当你尝试重新启动它时,它会给你IllegalStateException
。
您建议的异常不能多次执行相同的AsyncTask。 在for循环下初始化反馈对象,然后执行
根据AsyncTask线程规则
该任务只能执行一次(如果尝试第二次执行,则会抛出异常。)
它说: Cannot execute task: the task has already been executed (a task can be executed only once)
。 也就是说,您创建一个AsyncTask,执行一次 ,并将其垃圾收集。 对于同一实例,不能多次调用execute()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.