简体   繁体   English

for循环中的异步任务进度条

[英]Async Task progress bar in for loop

-I have tried to hit web service using Async Task method. - 我试图使用异步任务方法来访问Web服务。

-Here the for loop will dismiss the progress dialog from the first and crashes the Application. - 这里for循环将从第一个中删除进度对话框并使应用程序崩溃。 Kindly check this code and give suggestions. 请检查此代码并提供建议。

Code

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)
        {

        }
    }


 });

ERROR 错误

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)

Create a new AsyncTask instance for every iteration. 为每次迭代创建一个新的AsyncTask实例。

new AsyncTask().execute(value);

The reason being that you cannot execute a thread again, because once the thread is done with the run() method, you cannot restart it. 原因是您无法再次执行线程,因为一旦线程使用run()方法完成,您就无法重新启动它。 And that's why when you try to restart it, it gives you the IllegalStateException . 这就是为什么当你尝试重新启动它时,它会给你IllegalStateException

You cannot execute same AsyncTask multiple time as exception suggested. 您建议的异常不能多次执行相同的AsyncTask。 Initialize feedback object under the for loop and then execute 在for循环下初始化反馈对象,然后执行

According to AsyncTask Threading rules 根据AsyncTask线程规则

The task can be executed only once (an exception will be thrown if a second execution is attempted.) 该任务只能执行一次(如果尝试第二次执行,则会抛出异常。)

It says: Cannot execute task: the task has already been executed (a task can be executed only once) . 它说: Cannot execute task: the task has already been executed (a task can be executed only once) That is, you create an AsyncTask, execute it once , and let it be garbage-collected. 也就是说,您创建一个AsyncTask,执行一次 ,并将其垃圾收集。 You cannot call execute() more than once for the same instance. 对于同一实例,不能多次调用execute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM