【问题标题】:Application stops on changing page应用程序在更改页面时停止
【发布时间】:2013-05-10 08:10:42
【问题描述】:

我在一个应用程序中工作,我从服务器获得持续响应,但是当我更改页面或移动到另一个屏幕时,应用程序停止。我正在接收来自服务器的响应,但它没有在列表视图中更新。

我也使用了onResume 和onPause 方法,但我仍然收到错误消息。

代码;

protected class GetTask extends AsyncTask<Void, Void, Integer> {

        @Override
        protected Integer doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {
                CallReceiveMsgAPIService();
            } catch (Exception e) {
            }
            return 0;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            try {
                if (initiate != 1) {
                    mMessageHandle.sendEmptyMessage(0);
                    new GetTask().execute();

                    }
                } else {


                    mLiveChatList.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    mLiveChatList.requestLayout();
                                        alert();
                }
            } catch (Exception e) {
            }

        }
    }

错误:

05-10 14:14:01.213: E/AndroidRuntime(277): FATAL EXCEPTION: main
05-10 14:14:01.213: E/AndroidRuntime(277): 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(2131361857, class android.widget.ListView) with Adapter(class com.$ChatListAdapter)]
05-10 14:14:01.213: E/AndroidRuntime(277):  at android.widget.ListView.layoutChildren(ListView.java:1492)
05-10 14:14:01.213: E/AndroidRuntime(277):  at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:1960)
05-10 14:14:01.213: E/AndroidRuntime(277):  at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:591)
05-10 14:14:01.213: E/AndroidRuntime(277):  at android.view.ViewRoot.ensureTouchModeLocally(ViewRoot.java:2021)
05-10 14:14:01.213: E/AndroidRuntime(277):  at android.view.ViewRoot.ensureTouchMode(ViewRoot.java:2005)
05-10 14:14:01.213: E/AndroidRuntime(277):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1774)
05-10 14:14:01.213: E/AndroidRuntime(277):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 14:14:01.213: E/AndroidRuntime(277):  at android.os.Looper.loop(Looper.java:123)
05-10 14:14:01.213: E/AndroidRuntime(277):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-10 14:14:01.213: E/AndroidRuntime(277):  at java.lang.reflect.Method.invokeNative(Native Method)
05-10 14:14:01.213: E/AndroidRuntime(277):  at java.lang.reflect.Method.invoke(Method.java:521)
05-10 14:14:01.213: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-10 14:14:01.213: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-10 14:14:01.213: E/AndroidRuntime(277):  at dalvik.system.NativeStart.main(Native Method)

谁能帮我摆脱这个..@谢谢

【问题讨论】:

  • onResume 和 onPause 为空。有什么必要发帖?发布您的 logcat 和 相关 sn-p of code
  • @blackbelt 感谢您的回复,我会删除它
  • @blackbelt 在这里我添加了滚动时遇到的错误
  • 您无法从后台线程更新您的 UI,请确保您没有这样做
  • 现在发布相关代码的sn-p

标签: android listview


【解决方案1】:

列表发生变化后调用adapter.notifyDataSetChanged();通知listView列表内容发生变化。这肯定会解决这个错误。

protected class GetTask extends AsyncTask<Void, Void, Integer> {
public interface TaskListener
{
public void updateResult(Object result);
}
private TaskListener listener;
        @Override
        protected Integer doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {
                CallReceiveMsgAPIService();
            } catch (Exception e) {
            }
            return 0;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            try {
                if (initiate != 1) {
                    mMessageHandle.sendEmptyMessage(0);
                    new GetTask().execute();

                    }
                } else {

          listener.updateResult(result);
                    //mLiveChatList.setAdapter(adapter);
                    //adapter.notifyDataSetChanged();
                   // mLiveChatList.requestLayout();
                                      //  alert();
                }
            } catch (Exception e) {
            }

        }
    }

在activity中实现这个接口TaskListener,在updateresult方法中做代码注释。

【讨论】:

  • 那么您的服务器响应处理可能有一些错误,您可以添加代码sn-p来详细说明。
  • 您尝试在异步线程上更改适配器,这是错误的,做一件事添加一个侦听器并将其发送回活动,并在其中执行 else 部分的代码
  • 同样的错误??实际上我在我的一个应用程序中做了同样的事情,而且效果很好..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 2020-08-30
  • 2014-09-06
  • 2022-08-05
  • 1970-01-01
相关资源
最近更新 更多