计时器按钮的机器人集可视性可视性、计时器、机器人、按钮

2023-09-04 13:01:55 作者:清酒暖风

我有一个应用程序,显示了一个声明在程序的开始。我想一个按钮保持不可见的时间设定量,然后变得可见。 我成立了一个线程休眠5秒钟,然后试图使该按钮可见。然而,我当我执行我的code得到此错误:

08-02 21:34:07.868:ERROR / AndroidRuntime(1401):android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程

我怎么能指望5秒,然后使按钮可见? 谢谢你。

 发splashTread =新的Thread(){
           @覆盖
           公共无效的run(){
            尝试 {
                   INT等待= 0;
                   而(_active&安培;&安培;!(_ok2)){
                       睡眠(100);
                       如果(_active){
                           等待+ = 100;
                           如果(等待> = _splashTime)
                           {
                            turnButtonOn();
                           }

                       }
                   }
               }赶上(InterruptedException异常E){
                   // 没做什么
               } 最后 {
                   完();
                   startActivity(新的意向书(com.lba.mixer.Choose));

               }
    };
    splashTread.start();


      公共静态无效turnButtonOn(){
         okButton.setVisibility(View.VISIBLE);
      }
 

解决方案

现在的问题是,你是不是当你调用UI线程 okButton.setVisibility(View.VISIBLE); ,因为你创建和运行自己的线程。什么,你所要做的就是让你的按钮的处理程序,并设置通过您通过处理程序获得UI线程的知名度。

因此​​,而不是

  okButton.setVisibility(View.VISIBLE)
 

你应该做的。

  okButton.getHandler()后(新的Runnable(){
    公共无效的run(){
        okButton.setVisibility(View.VISIBLE);
    }
});
 
按键计时器电子价格 按键计时器电子批发 按键计时器电子厂家

I have an app that shows a disclaimer at the beginning of the program. I want a button to remain invisible for a set amount of time, and then become visible. I set up a thread that sleeps for 5 seconds, and then tries to make the button visible. However ,I get this error when I execute my code:

08-02 21:34:07.868: ERROR/AndroidRuntime(1401): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

How can I count 5 seconds, and then make the button visible? THanks.

Thread splashTread = new Thread() {
           @Override
           public void run() {
            try {
                   int waited = 0;
                   while(_active && (!_ok2)) {
                       sleep(100);
                       if(_active) {
                           waited += 100;
                           if(waited >= _splashTime)
                           {
                            turnButtonOn();
                           }

                       }
                   }
               } catch(InterruptedException e) {
                   // do nothing
               } finally {
                   finish();
                   startActivity(new Intent("com.lba.mixer.Choose"));

               }
    };
    splashTread.start();


      public static void turnButtonOn() {
         okButton.setVisibility(View.VISIBLE);
      }

解决方案

The problem is that you're not in the UI thread when you call okButton.setVisibility(View.VISIBLE);, since you create and run your own thread. What you have to do is get your button's handler and set the visibility through the UI thread that you get via the handler.

So instead of

okButton.setVisibility(View.VISIBLE)

you should do

okButton.getHandler().post(new Runnable() {
    public void run() {
        okButton.setVisibility(View.VISIBLE);
    }
});