这是我第一次尝试将AsyncTask用于进度条。我使用三种方法:
onPostExecute()
,onPreExecute()
和doInBackground()
。
这是我的班级:
public class BackgroundAsyncTask extends
AsyncTask<Void, Integer, Void> {
private ProgressDialog dialog;
int myProgress;
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
dialog.dismiss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog = ProgressDialog.show(nea.this, "", "Loading. Please wait...", true);
myProgress = 0;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
loadFeed();
return null;
}
}
不幸的是,这不是running.loadFeed();是加载rss feed的类。 这是我的logCat:
11-28 02:59:57.968: E/OSFP.News(409): Only the original thread that created a view hierarchy can touch its views.
11-28 02:59:57.968: E/OSFP.News(409): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-28 02:59:57.968: E/OSFP.News(409): at android.view.ViewRoot.checkThread(ViewRoot.java:3020)
11-28 02:59:57.968: E/OSFP.News(409): at android.view.ViewRoot.invalidateChild(ViewRoot.java:647)
11-28 02:59:57.968: E/OSFP.News(409): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:673)
11-28 02:59:57.968: E/OSFP.News(409): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
11-28 02:59:57.968: E/OSFP.News(409): at android.view.View.invalidate(View.java:5332)
11-28 02:59:57.968: E/OSFP.News(409): at android.widget.AbsListView.resetList(AbsListView.java:1192)
11-28 02:59:57.968: E/OSFP.News(409): at android.widget.ListView.resetList(ListView.java:560)
11-28 02:59:57.968: E/OSFP.News(409): at android.widget.ListView.setAdapter(ListView.java:489)
11-28 02:59:57.968: E/OSFP.News(409): at android.app.ListActivity.setListAdapter(ListActivity.java:265)
11-28 02:59:57.968: E/OSFP.News(409): at kostas.menu.olympiakos.nea.loadFeed(nea.java:99)
11-28 02:59:57.968: E/OSFP.News(409): at kostas.menu.olympiakos.nea.access$0(nea.java:74)
11-28 02:59:57.968: E/OSFP.News(409): at kostas.menu.olympiakos.nea$BackgroundAsyncTask.doInBackground(nea.java:225)
11-28 02:59:57.968: E/OSFP.News(409): at kostas.menu.olympiakos.nea$BackgroundAsyncTask.doInBackground(nea.java:1)
11-28 02:59:57.968: E/OSFP.News(409): at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-28 02:59:57.968: E/OSFP.News(409): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-28 02:59:57.968: E/OSFP.News(409): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-28 02:59:57.968: E/OSFP.News(409): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-28 02:59:57.968: E/OSFP.News(409): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-28 02:59:57.968: E/OSFP.News(409): at java.lang.Thread.run(Thread.java:1019)
11-28 02:59:57.984: W/dalvikvm(409): threadid=11: thread exiting with uncaught exception (group=0x40015578)
11-28 02:59:58.000: E/AndroidRuntime(409): FATAL EXCEPTION: AsyncTask #2
11-28 02:59:58.000: E/AndroidRuntime(409): java.lang.RuntimeException: An error occured while executing doInBackground()
11-28 02:59:58.000: E/AndroidRuntime(409): at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-28 02:59:58.000: E/AndroidRuntime(409): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
11-28 02:59:58.000: E/AndroidRuntime(409): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
11-28 02:59:58.000: E/AndroidRuntime(409): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
11-28 02:59:58.000: E/AndroidRuntime(409): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-28 02:59:58.000: E/AndroidRuntime(409): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-28 02:59:58.000: E/AndroidRuntime(409): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-28 02:59:58.000: E/AndroidRuntime(409): at java.lang.Thread.run(Thread.java:1019)
11-28 02:59:58.000: E/AndroidRuntime(409): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-28 02:59:58.000: E/AndroidRuntime(409): at android.os.Handler.<init>(Handler.java:121)
11-28 02:59:58.000: E/AndroidRuntime(409): at android.widget.Toast.<init>(Toast.java:68)
11-28 02:59:58.000: E/AndroidRuntime(409): at android.widget.Toast.makeText(Toast.java:231)
11-28 02:59:58.000: E/AndroidRuntime(409): at kostas.menu.olympiakos.nea.loadFeed(nea.java:121)
11-28 02:59:58.000: E/AndroidRuntime(409): at kostas.menu.olympiakos.nea.access$0(nea.java:74)
11-28 02:59:58.000: E/AndroidRuntime(409): at kostas.menu.olympiakos.nea$BackgroundAsyncTask.doInBackground(nea.java:225)
11-28 02:59:58.000: E/AndroidRuntime(409): at kostas.menu.olympiakos.nea$BackgroundAsyncTask.doInBackground(nea.java:1)
11-28 02:59:58.000: E/AndroidRuntime(409): at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-28 02:59:58.000: E/AndroidRuntime(409): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-28 02:59:58.000: E/AndroidRuntime(409): ... 4 more
答案 0 :(得分:0)
您无法在Android中的非UI线程内创建或访问UI。根据您的调用堆栈,您将在线程中创建Toast对话框。我认为这让它破裂了。