asmgx
asmgx

Reputation: 8014

android: Variable in runOnUiThread

Hi i m using Android Studio, and I am using Deprecated function in a runOnUiThread It forces me to use a a Final Variable inside the runOnUiThread this is ok for the new function but for the Deprecated function i get an error

Error:(133, 16) error: incompatible types
required: Thread
found:    void

anyone can help to fix this.

    Thread thread = new Thread() {
        public void run() {
            try {
                String sURL = "http://pollsdb.com/schlogger/223.png";

                URL url = null;
                url = new URL(sURL);
                assert url != null;

                Bitmap bmp = null;
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                bmp = Bitmap.createScaledBitmap(bmp, 200, 200, false);
                BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bmp);

                final BitmapDrawable fbmpdw = bitmapDrawable;

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            ib1.setBackground(fbmpdw);
                        } else {
                            ib1.setBackgroundDrawable(fbmpdw); // <---- this the problem
                        }
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();

Upvotes: 1

Views: 1063

Answers (2)

Kartik_Koro
Kartik_Koro

Reputation: 1287

Do you need to use the thread variable anywhere else? I don't think you need to. If so, simply change in your first line

 Thread thread = new Thread() {

To

new Thread() {

Your .start() will return void, and you cannot do Thread thread=//some thing void as it is expecting some Thread type.

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157457

your problem is not the setBackgroudDrawable method, but Thread.start(), which returns nothing (void), while you are trying to assign its return value to the thread local variable

You can both change Thread thread = new Thread() { ... } and then call thread.start() or simply new Thread() { ... }.start(); without assignment

Upvotes: 3

Related Questions