0

我有个问题 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)
4

2 回答 2

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;
       }

    }
于 2013-11-02T15:49:01.193 回答
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也不允许多次启动。

但是还有一些我不清楚的事情:

  1. 为什么要在异步任务中执行 getRows() ?
  2. 这是Adapter.getRows(page)做什么的(因为这应该是静态方法)?是否改变了什么?
  3. 你一般想达到什么目标?你想一次只拥有一个 AsyncTask 吗?

还有一点需要注意:我没有看到您更改 AsyncTask 中的适配器内容,但那里(如例外所说)应该是......

如果您在其他地方执行此操作,请使用该方法onPostExecution或使用该runOnUiThread方法。

runOnUiThread(new Runnable() {

public void run() 
{
    //Update UI here                        
}
});
于 2013-11-02T17:30:42.810 回答