Need help with Timer

Put your problem here if it does not fit any of the other categories.

Need help with Timer

Postby Mag1c » Mon Feb 07, 2011 9:28 am

Hey,
I'm trying to make a really simple timer where it adds 1 every 2 seconds in an EditText:
Code: Select all
public class timeractivity extends Activity {
   Timer timer;
   Button b1;
   public  EditText edittext;
   int x = 0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        b1 = (Button)findViewById(R.id.Button01);
        edittext = (EditText)findViewById(R.id.EditText01);
       
        b1.setOnClickListener(new View.OnClickListener() {         
         @Override
         public void onClick(View v) {
             timer.schedule(new RemindTask(), 2000);
         }
      });           

       
       
    }
    class RemindTask extends TimerTask {
        public void run() {           
           x+=1;
            edittext.setText(x);
        }
   
}
}


But when I click on the button I get FC..
Why is this happening?
Thanks
Mag1c
Developer
Developer
 
Posts: 28
Joined: Tue Jan 04, 2011 12:15 am

Top

Re: Need help with Timer

Postby Chitra » Tue Feb 08, 2011 1:51 pm

Hey,
Instead, to set the text of a view using an integer, you need to do:

Code: Select all
edittext.setText(Integer.toString(x));
Thanks,
Chitra
User avatar
Chitra
Master Developer
Master Developer
 
Posts: 213
Joined: Mon Mar 01, 2010 7:59 am
Location: Bangalore

Re: Need help with Timer

Postby Chitra » Tue Feb 08, 2011 1:52 pm

Also one more sample example by using TimerTask,

Code: Select all
public class MainActivity extends Activity {

   private Button b1;

   private EditText edittext;

   private int x = 0;

   private Timer timer = new Timer();

   private Handler handler = new Handler();

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      b1 = (Button) findViewById(R.id.Button01);
      edittext = (EditText) findViewById(R.id.EditText01);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            timer.scheduleAtFixedRate(new MyTimerTask(), 1000, 1000);
         }
      });

   }

   class MyTimerTask extends TimerTask {

      @Override
      public void run() {
         Log.d("MyTimerTask", "************Timer started!*************");
         handler.post(runnable);
      }
   }

   private Runnable runnable = new Runnable() {

      public void run() {
         setMyText();
      }

      private void setMyText() {
         x += 1;
         edittext.setText(Integer.toString(x));
      }
   };

}
Thanks,
Chitra
User avatar
Chitra
Master Developer
Master Developer
 
Posts: 213
Joined: Mon Mar 01, 2010 7:59 am
Location: Bangalore

Re: Need help with Timer

Postby Mag1c » Thu Feb 10, 2011 11:22 am

Chitra wrote:Hey,
Instead, to set the text of a view using an integer, you need to do:

Code: Select all
edittext.setText(Integer.toString(x));


I dont think that is the problem,
When I lunch the application (after fixing this thing), it still FC at start!
Mag1c
Developer
Developer
 
Posts: 28
Joined: Tue Jan 04, 2011 12:15 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 26 guests