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).
Related
I am working on app that updates data for every 8 secs and the update was done using Async task. I am using loops to achieve this condition
while(const_val > update_val) {
new Asynctask().execute();
Thread.sleep(8000);
}
const_val will be constant and will be not be changed by any other methods.lets say this value will be 5.update_val will be updated and decremented when Asynctask is called and let's the value will be 10. So , the while loop executes until the condition is true and asynctask ,sleep are called .
When I use above while loop in a general method then UI gets locked and if I use the same loop in another asynctask there was an error saying "Only original thread that created a view hierarchy can touch its view "
You need to change your code to start the AsyncTask and have it provide an update via its onPostExecute() method. By calling Thread.sleep() you are sleeping the main thread (or UI thread) of your app, which is not good. You do not ever want to block the main thread. This article may help you better understand AsyncTask and threading in Android: http://po.st/Cei3m2
I don't think you should use a surrounding loop. Look at this example:
http://javatechig.com/android/progress-notification-in-android-example
the AsyncTask is a private inner class
the onPostExecute updates the UI with a message/cancels the load bar
This way you don't have to loop and the onCreate() can return instantly.
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.
I am having a main class A, from which i create an object to class B extending Asynctask. Have set content view in A. Now in doInBackground of B, i want to update the textView of A, but its giving
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Exception. I tried declaring the setcontentview in B as well, but no use, can anyone please tell how to edit the textview of A in B??
override onProgressUpdate and call publishProgress in doInBackground
You need to call setText() from the UI thread, so either from onPreExecute(), onPostExecute() or in onProgressUpdate()
You could also use a runOnUiThread() handler in doInBackground(), but that kind of defeats the purpose of using an AsyncTask.
If the text update has to do with progress information, then you could use publishProgress()/onProgressUpdate() for that.
If it's not a progress update in a wider sense then I'm with Raghav -- you should consider another approach then.
We should not perform any ui changes in the doinBackgroud. You should and can perform in post execute. and also you can set notification in preexecute or onProgressUpdate.
Hope this will help you.
doInBackground is work thread. You have handle View in UI thread. please do it in onPreExecute(), onPostExecute() or onProgressUpdate. All these are working in UI Thread.
Of course you can pass Integer to onProgressUpdate. please notice the second paramters: AysncTask<Void, Integer, VOid>, the second parameter means the onProgressUpdate parameter type
I Have several activities handling diferent screens with information that is loaded asynchrounously via AsyncTask.
All data fetching are done in doInBackground()
All UI operations are done in onPostExecute()
In some instances I get the error: "Can't create handler inside thread that has not called Looper.prepare()"
If I do the Looper.prepare(), it crashes if a new activity is instanced for this class.
It's a weird behaviour that I'm able to find why it happens. The other screens with similar behaviour work as expected. There are no differences between them that I can find.
Has anyone had this behaviour?
"Can't create handler inside thread that has not called Looper.prepare()" means that you are trying to call AsyncTask.execute() outside of the UI thread.
There are few rules you should follow using AsyncTask:
The task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI thread.
Also it can mean than you are creating Handler object inside doInBackground method.
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