我想知道如何在进入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
我得到另一个例外来做到这一点......有什么想法吗?