rainbow
rainbow

Reputation: 43

About sub thread operation UI

Is the sub thread can operate UI? Or it can only use handler to operate in main thread. But I use this code. It did not launch the error. Is anyone has ideas?

new Thread(new Runnable() {
            public void run() {
                TextView tv=(TextView) findViewById(R.id.aaa);
                tv.setText("111");
            }
        }).start();

Upvotes: 0

Views: 101

Answers (2)

MByD
MByD

Reputation: 137332

Generally only the main thread should touch the UI. You are not promised to get an exception otherwise, but you are very likely to.

You should use handler / asyncTask / runOnUiThread as they are the ways to modify UI, other ways may work in some cases, but are not guaranteed.

Upvotes: 0

PC.
PC.

Reputation: 7024

No, you cannot perform UI operation from a different thread. If you want to update the UI, you must use handlers. Alternatively you can also use async tasks or Activity.runOnUiThread.

Upvotes: 1

Related Questions