在子线程 中更新UI的代码

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new Runnable() {
            
            @Override

            public void run() {

tvText.setText("other thread ");

                
            }
        }).start();
    }
这段代码在运行的过程中是正确的,因为它执行线程之前还没有创建好viewRootImp,如果加上线程休眠,他一定会报异常,说在主线程才能更新UI的操作

猜你喜欢

转载自blog.csdn.net/qq_34966875/article/details/51589606