0

我想知道如何在进入AsyncTask
内部的循环时更新 textview我做了什么:

    for (int i = 0; i < buffer.length; i += bufferLength) {
        pb.setMax(kbfilesize);
        int prog = i/bufferLength;
        pb.setProgress(prog);
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        textV.setText(pb.getProgress());
                    }
                });
            }
        }, 0, 100);
        outStream1.writeBytes(lineEnd + twoHyphens + boundary + twoHyphens + lineEnd);
    }

同样正如我所读到的,我在使用时不必runOnUiThread使用AsyncTask我可以从 更新 UI AsyncTask,但每次我尝试这样做时都会出现异常:

05-08 01:12:43.511: W/ResourceType(32344): No package identifier when getting value for resource number 0x00000000
05-08 01:12:43.521: W/dalvikvm(32344): threadid=11: thread exiting with uncaught exception (group=0x41eff2a0)
05-08 01:12:43.531: E/AndroidRuntime(32344): FATAL EXCEPTION: AsyncTask #1
05-08 01:12:43.531: E/AndroidRuntime(32344): java.lang.RuntimeException: An error occured while executing doInBackground()
05-08 01:12:43.531: E/AndroidRuntime(32344):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at java.lang.Thread.run(Thread.java:856)
05-08 01:12:43.531: E/AndroidRuntime(32344): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
05-08 01:12:43.531: E/AndroidRuntime(32344):    at android.content.res.Resources.getText(Resources.java:242)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at android.widget.TextView.setText(TextView.java:3800)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at com.linesmedia.MainActivity$FileUploadTask.doInBackground(MainActivity.java:255)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at com.linesmedia.MainActivity$FileUploadTask.doInBackground(MainActivity.java:1)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-08 01:12:43.531: E/AndroidRuntime(32344):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-08 01:12:43.531: E/AndroidRuntime(32344):    ... 4 more

即使我使用runOnUiThread我得到另一个例外来做到这一点......有什么想法吗?

4

3 回答 3

3

您不需要在 doInBackground 中发布 Runnable。AsyncTask有一个方法叫onProgressUpdate()你应该为进度更新实现。这将在 UI 线程上调用。实现该方法并publishProgress()在循环结束时添加。

您也不应该在doInBackground(). 所有这些都需要在 UI 线程上发生,所以onProgressUpdate()改为执行。

于 2013-05-07T22:26:28.587 回答
3

Uuuuhhhh....使用onProgressUpdate()

http://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate(Progress...)

于 2013-05-07T22:26:43.333 回答
2

这里实际发生的textV.setText(pb.getProgress());是调用

setText(int resourceId)

不是

setText(String text)

你想打电话:

textV.setText(Integer.toString(pb.getProgress()));

代替

textV.setText(pb.getProgress());
于 2013-05-07T22:28:57.387 回答