IllegalStateException when updating adapter data - android

Please, help me to find a fix for this bug, where it appears in the production version and I don't know where is the bug exactly from the following log.
I got the following log report from Crashlytics:
ListView.java line 1557
android.widget.ListView.layoutChildren
Fatal Exception: 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(2131558802, class
android.widget.ListView) with Adapter(class
android.widget.HeaderViewListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1557)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3442)
at android.view.View.dispatchTouchEvent(View.java:7565)
.
.
.

You can only change the data from the main thread.
If you are getting data in a background thread, either wait for the result in the main thread, change the data, and call adapter.notifyDataSetChanged(), or go back to the main thread from your background thread using a handler and a run() method.

Related

Listview did not receive notification

I am getting this error in one of my ListView. The thing is I am calling notifyDataSetChanged() but still I am getting this error. could any one guide me what I am doing wrong.
getting exception:
Fatal Exception: 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. Make sure your adapter calls notifyDataSetChanged() when its content changes.
[in ListView(2131034328, class android.widget.ListView) with Adapter(class com.activities.RestaurentListActivity$CustomAdapterRestaurant)]

Wrong Thread Exception

whenever I am using
listview.scrollTo(int x,int y) method I am getting an Exception called
android.view.View Root Implementation Called From Wrong Thread Exception: only the original thread that created a hierarchy can touch its views.
May I know what is the problem and what is the solution for this exception?
To update the listview, you need to update it from the main thread. You can throw the information to the main thread and update it, using a Handler.

getting sporadic ViewRoot$CalledFromWrongThreadException

I have an app with a long running action. I am using AsyncTask. In its onPostExecute() I call a function that modifies some views. I am getting this error on calling setEnabled on the 5th view of 10 views being modified. And it happens very seldom, getting the odd error report from users.
Obviously a multithreading issue. But I thought onPostExecute() always runs on the UI thread? Could it be that the UI thread suddenly gets changed to another thread??
Stack Trace:
generated the following exception:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original
thread that created a view hierarchy can touch its views.
--------- Instruction Stack trace ---------
android.view.ViewRoot.checkThread(ViewRoot.java:3041)
android.view.ViewRoot.invalidateChild(ViewRoot.java:647)
android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:673)
android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
android.view.View.invalidate(View.java:5255)
android.view.View.invalidateDrawable(View.java:7293)
android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:300)
8.
android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:227)
9.
android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:99)
android.graphics.drawable.Drawable.setState(Drawable.java:400)
android.view.View.drawableStateChanged(View.java:7374)
android.view.ViewGroup.drawableStateChanged(ViewGroup.java:3357)
android.widget.FrameLayout.drawableStateChanged(FrameLayout.java:164)
android.view.View.refreshDrawableState(View.java:7388)
android.view.View.setEnabled(View.java:3147)
com.voltup.powermax.ac.a(ActivityAppUiProxy.java:383)
com.voltup.powermax.cp.onPostExecute(ModeChange.java:1)
android.os.AsyncTask.finish(AsyncTask.java:417)
But I thought onPostExecute() always runs on the UI thread?
Yes, it does. Per Selvin's comment, AFAIK, if you try to create an AsyncTask from another thread, it fails.
Could it be that the UI thread suddenly gets changed to another thread??
No. More likely, you are accidentally going down a code path in doInBackground() that is updating the UI.
If you have a full stack trace showing that you are getting this exception from logic executed in onPostExecute(), please edit your question and paste in that stack trace.

RuntimeException in AsyncTask

I am using AsyncTask for a long running task but I am getting the following error.
error in doInBackground====java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Why am I getting that error and what does it mean?
You can access GUI only from OnPostExecute and OnProgressUpdate.
You're probably running the task inside a non-UI thread.
The UI thread is set so that it loops. You can see where's the loop in a stack trace or in ddms while debugging:
...
MessageQueue.next() line: 146
Looper.loop() line: 110
...
This loop takes care of handling the messages, like the ones that an AsyncTask sends, with a Handler, like the one the AsyncTask is trying to set. If you're launching the task on a thread that has no looper set you get that message.
Usually this error comes when you change UI from asyntask which is not allowed, other way is to implement handler instead asyntask...
Because you don't post your code, so hard to find issue.
I think because you access UI Thread inDoInBackground. (for example: you access TextView, or Button, or EditText....) This is a most common error when use Asyntask.
you should post some parameter to publishProgress in DoInBackGround. then asyntask will send this param to onProgressUpdate and this method will act on UI Thread

problem with calling invalidate in async task thread in Android !

I am trying to call the invalidate() from asyntask thread. I am getting this error :
10-18 15:14:30.469: ERROR/AndroidRuntime(889): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
The line I have used is :
mainClass.myMapView.invalidate();//where mainClass=main UI class
Can anyone kindly suggest where my fault is ?
Thanks.
-
ahsan
You can't do anything UI-related from a different thread than the UI thread (thus its name). You should call invalidate() in either onPostExecute() or in onProgress(). Or, use runOnUiThread() (which is basically what publishProgress() / onProgress() does).

Categories

Resources