我有一个问题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)
答案 0 :(得分:0)
如果我理解正确(根据您的尝试),您希望在运行时避免调用AsyncTask。这应该做的工作:
// class variable
// boolean isRunning = false;
protected void onNextPageRequested(int page) {
if (!isRunning) {
isRunning = true;
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);
isRunning = false;
}
}
答案 1 :(得分:0)
根据您的LogCat错误,您可以在另一个线程上修改ListView数据,然后修改主线程..正如它所说:
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)]
也不允许多次启动单个AsyncTask
。
但还有一些事情我不清楚:
Adapter.getRows(page)
做了什么(因为这应该是静态方法)?确实改变了什么?还有一点需要注意:我没有看到你在AsyncTask中更改适配器内容,但那里(如例外)应该是...
如果您在其他地方执行此操作,请使用onPostExecution
或使用runOnUiThread
方法。
runOnUiThread(new Runnable() {
public void run()
{
//Update UI here
}
});