GVillani82
GVillani82

Reputation: 17429

Only the original thread that created a view hierarchy can touch its views

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.

Upvotes: 0

Views: 918

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

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.

Upvotes: 2

Related Questions