Click here to Skip to main content
12,827,276 members (47,498 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java Mobile Android
Hey guys I'm learning android by making an simple app I though up. What I'm trying to make is an simple app that every second lights up a button then a second later the light turns off, if the user hits the button while its lit up they get a point. the button which gets lit up depends on a random number generator.

I'm currently using a tutorial I found on ScheduledExecutorService to create a timer, the program outputs correctly in the console but gets stuck after updating the program once after I added the code to update a label every tick. Heres the code

private final ScheduledExecutorService scheduler =   Executors.newScheduledThreadPool(1);    
  public void beepForAnHour() 
	  final Runnable beeper = new Runnable() 
		     public void run() 
		    	// int Min = 1;
		    	// int Max = 3;
		    	// randomNum = (int) (Math.random() * ( Max - Min ));		
		    	 System.out.println(Integer.toString(time)+" test"); 

                       //without these 2 lines the timer keeps increasing correctly	
		    	 TextView Time = (TextView)findViewById(;
      final ScheduledFuture beeperHandle =   scheduler.scheduleAtFixedRate(beeper, 0, 1, SECONDS);
      scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 60 * 60, SECONDS);

Any suggestions why this is a problem? And any advise on lighting the button up and off again? I planned to have a random number generator light up a button depending on the number generated.

I imagine I would also have to have if statement on the cancel part of the timer so when "lives = 0" the timer stops and the game ends although that can be looked at later. So now I just need to understand what I am doing wrong
Posted 26-Dec-12 3:26am
Updated 26-Dec-12 3:28am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Put a try/catch around those 2 lines and you will see that TextView is probably not initalized at that point of your app's life cycle.

That's because the function findViewById(int) out of the class
[^] can not return a valid value at that point.
Please read in the Documentation of Activity.
Phoenix234 26-Dec-12 14:04pm
I thought I put it in the same class but I will have a look and get back to you.

Is it common for the SDK not to give errors on this kind of thing? It seems like something that usually gets a red line under it
TorstenH. 27-Dec-12 12:07pm
the SDK would bring an exception - if it had the chance to.
Wrap the code in a try/catch and you'll get the exception. It's suppressed otherwise.
Phoenix234 29-Dec-12 18:36pm
Ah Yes I see that now, how would I update the timer UI then?

Also while this timer is running I want timer to be created everytime a button lights up and 1 second later the timer get destroyed and the button go back to normal. suggestions on what I should be looking at?
TorstenH. 30-Dec-12 2:38am
I would extend the button and give it that default-behavior you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170326.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100