Android: Accessing UI Element from timer thread

后端 未结 3 665
庸人自扰
庸人自扰 2020-12-01 14:05
public Button stb;
static int cnt=0;
public ArrayList Butgrp1 = new ArrayList();
Timer myt; 
TimerTask t;
stb.setOnClickListene         


        
3条回答
  •  自闭症患者
    2020-12-01 14:42

    You don't need to call runOnUIThread inside the handler. By calling post on the Handler instance, the runnable you pass will be executed on the UI thread at some point in the future. Change your code to look like this and it should work:

     Handler h=new Handler();
    
        h.post(new Runnable() {
    
            public void run() {
    
                        // TODO Auto-generated method stub
                        Butgrp1.get(cnt).setChecked(true);
                        cnt=cnt+1;
                        if(cnt>4)
                            cnt=0;
                        if(cnt>0)
                        //  Butgrp1.get(cnt-1).setChecked(false);
                        System.out.println(cnt);
                    }
                });
    

提交回复
热议问题