我有个问题 android listview中的AsyncTask在正常运行的时候确实不是问题,所以在asyntask运行时用户向下滚动页面到下一页的情况,直接应用强制关闭。
我已经尝试了所有条件:
if (AsyncTask.Status.RUNNING == null) {
/ / My code do in backgroud here
}
if (backgroundtask.getStatus! = STATUS.RUNNING) {
/ / My code do in backgroud here
}
总是失败...
public abstract View getAmazingView(int position, View convertView,
ViewGroup parent);
public final View getView(int position, View convertView, ViewGroup parent) {
View res = getAmazingView(position, convertView, parent);
if (position == getCount() - 1 && automaticNextPageLoading) {
onNextPageRequested(page + 1);
}
final int section = getSectionForPosition(position);
boolean displaySectionHeaders = (getPositionForSection(section) == position);
bindSectionHeader(res, position, displaySectionHeaders);
return res;
}
protected void onNextPageRequested(int page) {
backgroundTask = new AsyncTask<Integer, Void, List<Bola>>() {
protected List<Bola> doInBackground(
Integer... params) {
int page = params[0];
return Adapter.getRows(page);
}
protected void onPostExecute(
List<Bola> result) {
if (isCancelled()){
backgroundTask.cancel(true);
}
list.addAll(result);
nextPage();
notifyDataSetChanged();
if (result.size() > 0) {
// still have more pages
notifyMayHaveMorePages();
} else {
notifyNoMorePages();
}
};
}.execute(page);
}
如何在 AsyncTask 运行条件下运行,并且用户使用向下滚动事件?
======= 更新我的日志 =======
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): FATAL EXCEPTION: main
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131034281, class com.dhika.bola.application.ui.ListUtils) with Adapter(class android.widget.HeaderViewListAdapter)]
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.widget.ListView.layoutChildren(ListView.java:1548)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3490)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at com.dhika.bola.application.ui.ListUtils.onTouchEvent(ListUtils.java:445)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.View.dispatchTouchEvent(View.java:7262)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1932)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1968)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1420)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.app.Activity.dispatchTouchEvent(Activity.java:2428)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1916)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.View.dispatchPointerEvent(View.java:7442)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3610)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3538)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4790)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4750)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4902)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4870)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4924)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.Choreographer.doFrame(Choreographer.java:546)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.os.Handler.handleCallback(Handler.java:725)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.os.Looper.loop(Looper.java:153)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at android.app.ActivityThread.main(ActivityThread.java:5297)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at java.lang.reflect.Method.invoke(Method.java:511)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-02 23:22:42.330: ERROR/AndroidRuntime(25830): at dalvik.system.NativeStart.main(Native Method)