How to set text of text view in another thread

后端 未结 4 1525
说谎
说谎 2020-11-30 07:42

I am tring to setText in another thread, that is, child thread. But for the following code, it is giving the error

Only the original thread that creat

4条回答
  •  伪装坚强ぢ
    2020-11-30 08:19

    Use runOnUiThread for updating the UI control. In your case:

    runningActivity.runOnUiThread(new Runnable() {
        public void run() {
            tv.setText(p + " %");
        }
    });
    

    Edited:

    Activity mActivity;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        mActivity= this;
       ...
       ..//The rest of the code
    } //close oncreate()
    
    thread{
        mActivity.runOnUiThread(new Runnable() {
            public void run() {
                tv.setText(p + " %");
            }
        });
    }
    

提交回复
热议问题