24
public Button stb;
static int cnt=0;
public ArrayList<RadioButton> Butgrp1 = new ArrayList<RadioButton>();
Timer myt; 
TimerTask t;
stb.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

myt.mschedule(new TimerTask() {

    @Override
    public void run() {

        // TODO Auto-generated method stub


        System.out.println("Entering run");
        Handler h=new Handler();

        h.post(new Runnable() {

            public void run() {

                // TODO Auto-generated method stub
                runOnUiThread(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);
                    }
                });


            }
        });

        //rg.getChildAt(cnt).setPressed(true);

    }
},1000,2000);

I need to access a group of radio buttons on the ui and set it as checked at regular intervals, but i keep getting different errors, i realized i must use a handler, but its still not working...can anyone please tell me where i am going wrong....am a newbie and am trying out stuff to understand the working better...please help...

Ajay
  • 1,796
  • 3
  • 18
  • 22

3 Answers3

26

You have to create the Handler in the UI Thread, i.e. in onCreate of your Activity.

Because you create it in the run method of a background thread, the handler will execute your code in that very same background thread.

You could also initialize your Handler directly:

public class MyActivity extends Activity{

    private Handler handler = new Handler();

    //more code
}

And then don't use runOnUIThread:

 handler.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);
                }
            });

EDIT: Ok try this cleaned up code. Because you did not post your full Activity this won't work out of the box:

public class TestActivity extends Activity {

    private Button button;
    static int cnt=0;
    public ArrayList<RadioButton> buttonArray = new ArrayList<RadioButton>();
    private Timer timer = new Timer(); 

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                timer.schedule(new MyTimerTask(), 1000,2000);
            }
        });
    }


    private void doButtonStuff(){
        buttonArray.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);
        }
    }

    private class MyTimerTask extends TimerTask{

        @Override
        public void run() {        
            runOnUiThread(new Runnable() {              
                @Override
                public void run() {
                    doButtonStuff();
                }
            });
        }       
    }
}
thaussma
  • 9,756
  • 5
  • 44
  • 46
  • Thanks for replying, i did what you asked me to do it enters the loop,but once the button is accessed it terminates giving the following error Fatal Exception-Timer 0 E/AndroidRuntime(407): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. – Ajay Mar 16 '12 at 14:23
  • 1
    @Ajay You need to make sure that you create your handler from the UI thread then you're good – wnafee Mar 16 '12 at 14:46
  • @wnafee I did create it from the ui thread, still i gt the error, i have a feeling that i am not implementing the timer task correctly – Ajay Mar 16 '12 at 14:58
  • 1
    It worked!! Thanks a ton :) so overriding the timertask run method worked, could u also pls explain why it worked? – Ajay Mar 16 '12 at 15:44
  • I still think the problem was that the Handler was not created in the UI Thread. Now there is no Handler any more. I really can't say more because you did not post the whole Activity. – thaussma Mar 16 '12 at 16:31
1

You can pass the Activity as a parameter to the method that runs the timertask, and then you can use Activity.runOnUiThread to execute your tasks in UI Thread. There are lots of post in stackoverflow site regarding the usage of runOnUiThread usage.

Navine
  • 21
  • 5
0

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);
                }
            });
Chris
  • 22,923
  • 4
  • 56
  • 50
  • Thanks for replying, i did what you asked me to do it enters the loop,but once the button is accessed it terminates giving the following error – Ajay Mar 16 '12 at 14:22