0

well I want to load five images in five imageviews in one single method in one activity. The images are in jpeg and are short files (500kb max) so I've tryed this code on the Oncreate:

new Thread(new Runnable() { 
            public void run(){
ins1 = (ImageView) findViewById(R.id.ins1);
//and four more of this imagesviews
ins5 = (ImageView) findViewById(R.id.ins5);

d1 = getResources().getDrawable(R.drawable.ins1);
//four more

ins1.setImageDrawable(d1);
// and four more

}

And Im getting this Log:

03-07 09:40:52.395: E/AndroidRuntime(3662): FATAL EXCEPTION: Thread-2727
03-07 09:40:52.395: E/AndroidRuntime(3662): Process: com.karlol.modernoapp, PID: 3662
03-07 09:40:52.395: E/AndroidRuntime(3662): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I've never worked with AsyncTask, that could be an option, please I need help

1 Answers1

0

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

This error is occuring because your are trying to bind a drawable to the imageview in worker thread and not in the UI thread.

Here is the example of asyntask

AsyncTask Android example

Community
  • 1
  • 1
Fahim
  • 12,198
  • 5
  • 39
  • 57