0

-我尝试使用异步任务方法访问 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)
4

3 回答 3

2

AsyncTask为每次迭代创建一个新实例。

new AsyncTask().execute(value);

原因是您无法再次执行线程,因为一旦线程使用该run()方法完成,您就无法重新启动它。这就是为什么当您尝试重新启动它时,它会为您提供IllegalStateException.

于 2013-04-02T05:44:23.980 回答
0

您不能按照异常建议多次执行相同的 AsyncTask。在for循环下初始化反馈对象然后执行

根据 AsyncTask 线程规则

该任务只能执行一次(如果尝试第二次执行将引发异常。)

于 2013-04-02T05:44:11.593 回答
0

它说:Cannot execute task: the task has already been executed (a task can be executed only once)。也就是说,您创建一个 AsyncTask,执行一次,然后对其进行垃圾收集。对于同一个实例,您不能execute()多次调用。

于 2013-04-02T05:44:20.027 回答