我FragmentActivity
用来交换两个标签。那是选项卡 A 和选项卡 B。在选项卡 B 中,我使用ListView
Loadmore 按钮,假设单击加载更多按钮时,我只是调用了背景线程。
private class loadMoreListView extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// Showing progress dialog before sending http request
pDialog = new ProgressDialog (
getActivity());
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
protected Void doInBackground(Void... unused) {
try {
//service url to fectch data..
//xml parsing
} catch (Exception e) {
response = "failure";
System.out.println(" sec XML Pasing Excpetion = " + e);
}
int currentPosition = listViewattlist.getFirstVisiblePosition();
// Appending new data to menuItems ArrayList
adapter = new CustomAdapter(getActivity(), R.layout.att_list, R.id.title, data);
listViewattlist.setAdapter(adapter);
// Setting new scroll position
listViewattlist.setSelectionFromTop(currentPosition + 1, 0);
return (null);
}
protected void onPostExecute(Void unused) {
// closing progress dialog
pDialog.dismiss();
}
}
当我交换并单击该按钮时,我可以看到进度条,但它显示如下错误:
07-13 11:42:29.386: E/AndroidRuntime(894): FATAL EXCEPTION: AsyncTask #2
07-13 11:42:29.386: E/AndroidRuntime(894): java.lang.RuntimeException: An error occured while executing doInBackground()
07-13 11:42:29.386: E/AndroidRuntime(894): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-13 11:42:29.386: E/AndroidRuntime(894): at java.lang.Thread.run(Thread.java:856)
07-13 11:42:29.386: E/AndroidRuntime(894): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.View.invalidate(View.java:10250)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.View.invalidate(View.java:10205)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.ImageView.invalidateDrawable(ImageView.java:190)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:350)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.graphics.drawable.Drawable.setVisible(Drawable.java:546)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.ImageView.onDetachedFromWindow(ImageView.java:1173)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.View.dispatchDetachedFromWindow(View.java:11789)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2532)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:3819)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.AbsListView.resetList(AbsListView.java:1936)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.ListView.resetList(ListView.java:502)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.ListView.setAdapter(ListView.java:442)
07-13 11:42:29.386: E/AndroidRuntime(894): at com.owentech.testswipeab.TabTwo$loadMoreListView.doInBackground(TabTwo.java:372)
07-13 11:42:29.386: E/AndroidRuntime(894): at com.owentech.testswipeab.TabTwo$loadMoreListView.doInBackground(TabTwo.java:1)
07-13 11:42:29.386: E/AndroidRuntime(894): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-13 11:42:29.386: E/AndroidRuntime(894): ... 5 more
------------------------------------------------------------------------