我想将文字附加到从doInBackground
返回的Edittext。
我的代码如下所示:
public void openFile() {
progressDialog.show();
new AsyncTask<Void,String,Void>(){
@Override
protected Void doInBackground(Void... voids) {
File file = new File("/some file");
BufferedReader br = new
BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
onProgressUpdate(line);
}
br.close();
} catch (IOException e) {
}
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
final String line = values[0].toString();
for (char c : line.toCharArray()) {
inputEditText.append(Character.toString(c));
}
}
@Override
protected void onPostExecute(Void val){
progressDialog.dismiss();
}
}.execute();
}
如果我删除onProgressUpdate
中的代码,则应用程序正在运行且没有错误。
如何解决这个问题??
错误:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRoot.checkThread(ViewRoot.java:3041)
at android.view.ViewRoot.invalidateChild(ViewRoot.java:647)
at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:673)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
at android.view.View.invalidate(View.java:5279)
at android.widget.TextView.updateAfterEdit(TextView.java:5104)
at android.widget.TextView.handleTextChanged(TextView.java:6617)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6775)
at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:259)
at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:28)
at android.widget.TextView.append(TextView.java:2334)
at android.widget.TextView.append(TextView.java:2321)
at com.example.sa.myapplication.MainActivity$7.onProgressUpdate(MainActivity.java:358)
答案 0 :(得分:1)
您直接从onProgressUpdate(line);
致电doInBackground
,您需要使用publishProgress
。
更改onProgressUpdate(line);
publishProgress(line);
http://developer.android.com/reference/android/os/AsyncTask.html#publishProgress%28Progress...%29
http://developer.android.com/intl/es/guide/components/processes-and-threads.html