即使我把它扔进AsyncTask的DoInBackground方法,我仍然会得到一个CalledFromWrongThreadException!谁能告诉我为什么以及如何解决它?
/**
* Recognizes key words in responseTexts and runs the appropriate function
* @param responseText
*/
private void responseHandler(String responseText) {
if(responseText.startsWith("Notes", 0)) {
notes.add(responseText);
new DownloadFilesTask().execute("WriteInternal" , null, null);
}
}
这是我的AsyncTask类
private class DownloadFilesTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... command) {
if(command[0].equals("WriteInternal") {
//write to internal storage
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput("notes.txt", Context.MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
for (int i = 0; i < notes.size(); i++){
osw.write(notes.get(i) + "\n");
}
osw.close();
fOut.close();
} catch(Exception e) {
e.printStackTrace(System.err);
}
notesListView.setAdapter(arrayAdapter); //refreshes the listView
}
else {
sendCommand(command[0]);
return null;
}
}
protected void onProgressUpdate(Void... progress) {}
protected void onPostExecute(String result) {}
}
这是我得到的堆栈跟踪:
05-22 15:14:20.370: E/AndroidRuntime(11730): FATAL EXCEPTION: AsyncTask #2
05-22 15:14:20.370: E/AndroidRuntime(11730): java.lang.RuntimeException: An error occured while executing doInBackground()
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.os.AsyncTask$3.done(AsyncTask.java:278)
05-22 15:14:20.370: E/AndroidRuntime(11730): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-22 15:14:20.370: E/AndroidRuntime(11730): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-22 15:14:20.370: E/AndroidRuntime(11730): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-22 15:14:20.370: E/AndroidRuntime(11730): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-22 15:14:20.370: E/AndroidRuntime(11730): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-22 15:14:20.370: E/AndroidRuntime(11730): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-22 15:14:20.370: E/AndroidRuntime(11730): at java.lang.Thread.run(Thread.java:856)
05-22 15:14:20.370: E/AndroidRuntime(11730): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:722)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:771)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4005)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.view.View.invalidate(View.java:8576)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.view.View.invalidate(View.java:8527)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.widget.AbsListView.resetList(AbsListView.java:1734)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.widget.ListView.resetList(ListView.java:502)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.widget.ListView.setAdapter(ListView.java:442)
05-22 15:14:20.370: E/AndroidRuntime(11730): at com.nelson.jarvisclientics.Notes$DownloadFilesTask.doInBackground(Notes.java:281)
05-22 15:14:20.370: E/AndroidRuntime(11730): at com.nelson.jarvisclientics.Notes$DownloadFilesTask.doInBackground(Notes.java:1)
05-22 15:14:20.370: E/AndroidRuntime(11730): at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-22 15:14:20.370: E/AndroidRuntime(11730): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-22 15:14:20.370: E/AndroidRuntime(11730): ... 5 more
答案 0 :(得分:5)
尝试使用runOnUIThread(new Runnable{/*code goes here*/}())
答案 1 :(得分:2)
您获得此异常的原因是您正在尝试更新AsyncTask的doInBackground()方法中的UI。当您在此处为列表视图设置适配器时会发生这种情况:notesListView.setAdapter(arrayAdapter); //refreshes the listView
您应该将其移至onPostExecute。更新onPostExecute中的UI并在必要时显示任何错误消息。
另请注意,您也在后台调用handleCommand();确保这也不会更新UI!
答案 2 :(得分:1)
您可以使用在UI线程上运行的onPostExecute ...这是使用asynctask的基础。