【问题标题】:timer.setText("setTextHere") does not work inside the threadtimer.setText("setTextHere") 在线程内不起作用
【发布时间】:2013-03-18 09:07:25
【问题描述】:

timer.setText("setTextHere") 在线程内部不起作用。

Thread thread1 = new Thread(){ 

            TextView timer;
            int t;
            public void run(){
                timer=(TextView) findViewById(R.id.timer);      
                try{
                    timer.setText("setTextHere");
                    sleep(5000);


                }
                catch(Exception e){ 
                    e.printStackTrace();                        
                }
                finally{
                    Intent new1 = new Intent("com.example.app1.MENU");
                    startActivity(new1);                    
                }                   
            }               
        };
        thread1.start();

【问题讨论】:

  • 它显示了什么???
  • 使用Activity.runOnUiThread从线程访问或更新用户界面
  • timer=(TextView) findViewById(R.id.timer);这应该在 UI 线程中。并且您只能在 UI 线程上执行 UI 更改。
  • 看看这些主题stackoverflow.com/…
  • 你必须在线程外声明textview!!!

标签: android multithreading settext


【解决方案1】:
_t = new Timer();
_t.scheduleAtFixedRate( new TimerTask() {
        @Override
        public void run() {
            _count++;
            runOnUiThread(new Runnable() //run on ui thread
             {
              public void run() 
              {
                _tv.setText( "" + _count );
              }
             });
        }
    }, 1000, 1000 ); 

【讨论】:

    【解决方案2】:

    您不能从后台线程触摸 UI。尝试使用 AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

    【讨论】:

      【解决方案3】:

      在你的 onCreate() 添加:

      h = new Handler();
      

      在某处添加:

      class SetText implements Runnable {
          public void run() {
             // do UI task
             timer.setText("setTextHere");
          }
      }
      

      在 someFunction() 上修改如下:

      ...
      try 
      {
          h.post( new SetText() );
          sleep(5000);
      }
      ...
      

      【讨论】:

        【解决方案4】:
        final TextView timer = (TextView) findViewById(R.id.timer);
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(2000);
                    timer.post(new Runnable() {
                        public void run() {
                            timer.setText("setTextHere");
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    Intent new1 = new Intent("com.example.app1.MENU");
                    startActivity(new1);
                }
            }
        }).start();
        

        【讨论】:

          【解决方案5】:

          您可以在初始化线程之前使用它并初始化 setText

              Thread thread1 = new Thread(){ 
          
          
                  int t;
                  public void run(){    
                      try{
                          sleep(5000);
          
          
                      }
                      catch(Exception e){ 
                          e.printStackTrace();                        
                      }
                      finally{
                          Intent new1 = new Intent("com.example.app1.MENU");
                          startActivity(new1);                    
                      }                   
                  }               
              };
              TextView timer;
              timer=(TextView) findViewById(R.id.timer);  
              timer.setText("setTextHere");
              thread1.start();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-07
            相关资源
            最近更新 更多