I have a an Activity in which I have a menu formed by three ImageView (LEFT,CENTER,RIGHT). I have two arrows (button), in order to move in the menu. When I click on the left arrow the image in the left ImageView is replaced with the image that was in the center ImageView; in a similar manner the image in the center ImageView is replaced with the image that was in the right ImageView, and for the right ImageView, a new image appears. When the activity is shown, I have not the required images the ImageView is shown with a default image,while the correct image is downloaded using Thread. After the three images are download I continue to have the three default image, and I can see the downloaded images only after movement on left or right. This, because "Only the original thread that created a view hierarchy can touch its views." So the thred used for download, when the download is ended can touch the view of my activity. What can I do for avoiding the problem? Maybe I have to use handler. But I'm not able to do this.
Asked
Active
Viewed 910 times
0
-
Why are you not able to use a handler ? – NikkyD Nov 10 '12 at 23:05
-
I have never use it. I have to try it. Have you any suggest? May be this resolve my problem? – GVillani82 Nov 10 '12 at 23:06
-
possible duplicate of [Android "Only the original thread that created a view hierarchy can touch its views."](http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi) – Alex L. Sep 05 '15 at 13:07
1 Answers
2
What can I do for avoiding the problem?
Use AsyncTask
instead of Thread
, with your update-the-ImageView
logic going in onPostExecute()
.
Or, use a Handler
.
Or, use post()
on your ImageView
.
Or, use runOnUiThread()
on your activity.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491