简体   繁体   English

应用程序在更改页面上停止

[英]Application stops on changing page

I am working in one application where i am getting continuous response from server but when i change the page or moved to the other screen the application gets stop.I am receiving the response from server but it is not updating in listview. 我正在一个应用程序中工作,在该应用程序中我不断收到服务器的响应,但是当我更改页面或移动到另一屏幕时,该应用程序停止了。我正在接收来自服务器的响应,但在列表视图中未更新。

I have used onResume and onPause method also but still i am receiving the error. 我也使用过onResumeonPause方法,但仍然收到错误。

code; 码;

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) {
            }

        }
    }

error: 错误:

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)

could anybody get me out of this ..@Thanks 有人可以让我摆脱这个.. @谢谢

Call adapter.notifyDataSetChanged(); 调用adapter.notifyDataSetChanged(); after there is a change in the list to notify the listView that the contents of list have changed. 列表发生更改后,通知listView列表的内容已更改。 This will surely solve this error. 这肯定会解决此错误。

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) {
            }

        }
    }

In activity implement this interface TaskListener and in the updateresult method do the code commented. 在活动中,实现此接口TaskListener,并在updateresult方法中执行代码注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM