setImageResource() programmatically to multiple ImageViews cause crash

I have 15 ImageViews created in the XML file and I have to set the images of them programmatically.

I have tried this code to do so:

imageView1.setImageResource(R.drawable.imageOne);

And it works fine until I try to do it with all 15 ImageViews:

imageView1.setImageResource(R.drawable.imageOne);
...
imageView15.setImageResource(R.drawable.imageFifteen);

Now the application crash when it tries to load the view, because of the code above. And I get this weird message: I/Choreographer: Skipped 46 frames! The application may be doing too much work on its main thread.

2 answers

  • answered 2018-04-14 15:54 Federico Rizzo

    Maybe you are delegating too work to MainThread so try to do it in a different way. Use multi-threading programming if possible. It's useful to make your app logic separated from the UI. Use Java threads, AsyncTask or something similar. Check documentation

  • answered 2018-04-14 15:54 S. K

    I think you are using scroll view, which is not supported to reusable views. If you want to create multiple imageviews and setting an images then you should use listview/RecyclerView etc which supports reusable view. Try to read about reusability.

    Your application may also crashed because of you are loading large image which is requires more memory to show. Read about: https://developer.android.com/topic/performance/graphics/load-bitmap.html