Home » Android » java – App Crashes on updating image via setImageResource()-Exceptionshub

java – App Crashes on updating image via setImageResource()-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I’m updating an image in Android Studio, but after executing the app is not responding. I’m creating a new thread then updating the image. Here’s my code section.

new Thread(new Runnable() {
    public void run() {
        ImageView image = (ImageView) findViewById(R.id.imageView);
        image.setImageResource(R.drawable.thin);
    }
}).start();
How to&Answers:

Please try like this, if you are using this method in fragment use getActivity() if you use in activity you can use it runOnUiThread like this directly.

ImageView image = (ImageView) findViewById(R.id.imageView);
     new Thread(new Runnable() {
                public void run() {

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            image.setImageResource(R.drawable.thin);
                        }
                    });
                }
            }).start();