Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:20:55+00:00 2026-05-31T12:20:55+00:00

public Button stb; static int cnt=0; public ArrayList<RadioButton> Butgrp1 = new ArrayList<RadioButton>(); Timer myt;

  • 0
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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:20:57+00:00Added an answer on May 31, 2026 at 12:20 pm

    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();
                    }
                });
            }       
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
public static IList<T> LoadObjectListAll<T>() { ISession session = CheckForExistingSession(); var cfg = new NHibernate.Cfg.Configuration().Configure();
public static Logger getLogger() { final Throwable t = new Throwable(); final StackTraceElement methodCaller
I am using following code to invoke Barcode scanner apps from Zing public Button.OnClickListener
#include <QtCore/QCoreApplication> #include <boost/bind.hpp> #include <boost/function.hpp> class button { public: boost::function<void()> onClick; boost::function<void(int ,double
I'm no Java guy, so I ask myself what this means: public Button(Light light)
Button class: class SubmitButton extends JButton implements ActionListener { public SubmitButton(String title){ super(title); ....
hi i have property named. public bool ShowLabels{get; set;} and one toolbar menu button
public class MyClass { public int Age; public int ID; } public void MyMethod()
public class Test { public static void main(String[] args) { } } class Outer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.