Android Thread modify EditText

后端 未结 5 2114
一向
一向 2021-01-14 04:34

I am having a problem with modifying EditText in another function started by the thread:

Thread thRead = new Thread( new Runnable(){
    public void run(){
          


        
5条回答
  •  别那么骄傲
    2021-01-14 05:00

    First of all take a look at your log, it usually contains a stack trace when an app shuts down.

    You shouldn't run the thread like you normally do, instead use runOnUiThread:

    Runnable thRead = new Runnable(){
       public void run() {
          EditText _txtArea = (EditText) findViewById(R.id.txtArea);
          startReading(_txtArea);
       }
    };
    runOnUiThread(thRead);
    

    The explaination: Only the UI thread is allowed to change the state of UI components.

提交回复
热议问题