67337

Application stops on changing page

Question:

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.

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

Answer1:

Call adapter.notifyDataSetChanged(); after there is a change in the list to notify the listView that the contents of list have changed. 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.

Recommend

  • How can I put Toast in a Runnable of a Service?
  • android:when run onLoadFinished in orientation change
  • How to convert to asynctask?
  • ArrayAdapter.getCount() NullPointerException
  • Getting data from Parse.com using fragments
  • Google Map Undesirably Resets camera zoom out
  • Android Generic AsyncTask Class
  • Android image didn't display to ImageView from image URL
  • Is it possible to run a playbook in “pull mode”?
  • Java AsyncTask passing variable to main thread
  • Confused about how to update my datasources on my UITableView after app has been released
  • Blackberry 6: how to detect a long click on track pad?
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • Deleting and Updating values from a cusrsor adapter
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Incrementing object id automatically JS constructor (static method and variable)
  • Eraser for UIBezierPath
  • How to limit post in wp_query
  • Upload files with Ajax and Jquery
  • Build own AppleScript numerical error handling
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Change div Background jquery
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?