I'm trying to get my timer to display on the screen. Whenever I run the program, I get a NullPointerException in 2 places. I commented the lines that had the errors
myCountDownTimer = new MyCountDownTimer(31000, 1000);
myCountDownTimer.onTick(31000); //NullPointerException on this line
myCountDownTimer.onFinish();
Here's MyCountDownTimer Class
import android.os.CountDownTimer;
import android.widget.TextView;
public class MyCountDownTimer extends CountDownTimer {
TextView textCounter;
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
textCounter.setText(String.valueOf(millisUntilFinished / 1000)); //NullPointerException on this line
}
#Override
public void onFinish() {
textCounter.setText("Finished");
}
}
Edit:
myCountDownTimer = new MyCountDownTimer(31000, 1000);
myCountDownTimer.thisText(textCounter);
myCountDownTimer.onTick(31000);
myCountDownTimer.onFinish();
and this was added to MyCountDownTimer
public TextView thisText(TextView textCounter){
return textCounter;
}
TextView textCounter; is null; if you created it in your activity then, create a constructor for your MyCountDownTimer and take TextView as parameter and put it in.. because if you create a TextView in your MyCountDownTimer its useless because you have to add it again to your content View..
like this
public class MyCountDownTimer extends CountDownTimer {
TextView textCounter;
public MyCountDownTimer(TextView textCounter, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.textCounter = textCounter;
}
// now you can use textCounter and not get an exception
Hope it helps
You have to initialize your object before you use it:
TextView textCounter = new TextView();
You have one NullPointerException, they on the same command.
Related
I'm working on a game where I need a countdown timer.
I need to be able to pause the timer, resume the countdown and to add some time to current countdown state.
I've looked at CountdownTimer class and its methods, but it seems like it doesn't have the required features.
I need advice - which component is best for this case?
How to use it?
What are the possible problems?
Thread? AsyncTask? Timer?
Does anyone have experience with this?
I think Thread can be used, but its easier to implement your features using CountdownTimer wrapper class:
static class MyCountdownTimer {
long mCurrentMilisLeft;
long mInterval;
CountdownTimerWrapper mCountdownTimer;
class CountdownTimerWrapper extends CountDownTimer{
public CountdownTimerWrapper(long millisInFuture,long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
}
#Override
public void onTick(long millisUntilFinished) {
mCurrentMilisLeft = millisUntilFinished;
}
}
public MyCountdownTimer(long millisInFuture, long countDownInterval) {
set(millisInFuture,countDownInterval);
}
public void pause(){
mCountdownTimer.cancel();
}
public void resume(){
mCountdownTimer = new CountdownTimerWrapper(mCurrentMilisLeft, mInterval);
mCountdownTimer.start();
}
public void start(){
mCountdownTimer.start();
}
public void set(long millisInFuture, long countDownInterval){
mInterval = countDownInterval;
mCurrentMilisLeft = millisInFuture;
mCountdownTimer = new CountdownTimerWrapper(millisInFuture, countDownInterval);
}
}
I am a new android developer. Recently, launched a word puzzle app - Wozzle
I want to add timer to the game so that a player gets a limited time to make a move. How to do that ? Also, the timer should be displayed to the player.
Try this,
public class CountDownTest extends Activity {
TextView tv; // textview to display the countdown
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
// 5000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000, 1000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
tv.setText("done!");
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText("Left: " + millisUntilFinished/1000);
}
}
}
Have a look at these links
A Stitch in Time
On screen timer in Android application?
stop watch logic
Android Game Programming: The Game Loop
I have an activity that starts after a certain time. Once this activity starts, the user is supposed to press a button that starts a new timer.
However, if the user does not press the button I want a toast to be displayed every 5 seconds until the button is pressed.
I'm trying to use a CountDownTimer. Here is the basics of the code I have so far:
I have no errors but no toasts are being displayed at the moment. Is it ok to have the MyCount class inside the BreakActivity class (if i put it outside I get an error).
Any help in getting this sorted will be greatly appreciated!
public class BreakActivity extends Activity {
Button startBreakButton; // button to start the break timer
Boolean clicked= false;
CountDownTimer counter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.breakscreen); // sets layout to breakscreen.xml
MyCount counter;
counter=new MyCount(5000,1000);
counter.start();
startBreakButton = (Button) findViewById(R.id.startBreakButton);
startBreakButton.setOnClickListener(mStartListener);
View.OnClickListener mStartListener = new OnClickListener() {
clicked=true;
//other listener code
};
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
if(clicked=false){
Toast.makeText(getApplicationContext(), "TAKE A BREAK", Toast.LENGTH_LONG).show();
counter= new MyCount(5000,1000);
counter.start();
}
}
#Override
public void onTick(long millisUntilFinished) {
long s1 = millisUntilFinished;
}
}
};
change :
if(clicked=false)
to
if(clicked==false)
Is there a timer functionality or timer that is a subclass of View in android that can be use to keep track the playing time of the user?
If I must build one, is it good to build a Thread or Runnable class that have a loop that put itself to sleep for 1000ms (Thread.sleep(1000)) then update itself. e.g.
public class TimerThread extends Runnable{
private int time;
boolean run = true;
public void run(){
while(run){
//update the View here
Thread.sleep(1000);
time++;
}
}
}
This might be helpful.
http://developer.android.com/reference/android/widget/Chronometer.html
I think you might need to Google for an example never really used it myself but I've heard of it.
Here is some code I use in one of my games. Hope it helps
MyCount counter = new MyCount(30000,10); //set to 30 seconds
counter.start();
public class MyCount extends CountDownTimer{
long time=0;
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
clock.setText("You lose!"); //clock is a textfield in the game
restart(); //call you own restart method
}
#Override
public void onTick(long millisUntilFinished) {
DecimalFormat df=new DecimalFormat("#.##"); //import java.text.DecimalFormat;
clock.setText("Left: "+ df.format(millisUntilFinished/1000.0));
time=millisUntilFinished;
}
public long getTime(){
return time;
}
}
Take look at.
http://developer.android.com/reference/android/os/CountDownTimer.html
Just to provide more options, here is another approach: Updating the UI from a Timer
I would be very grateful if someone could explain how to create a simple timer in a game which starts off at 30 seconds and then goes back until 0. At 0 I would then stop the game and invoke a method which would allow the next player to take a turn.
Any advice would be appreciated. Thanks
Edit:
import android.os.CountDownTimer;
import android.widget.TextView;
public class MyCount extends CountDownTimer {
static TextView timeDisplay;
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
timeDisplay.setText("Done!");
}
public void onTick(long millisUntilFinished) {
timeDisplay.setText("Left: " + millisUntilFinished / 1000);
}
}
In a seperate class I have created a method:
public void setTimer() {
timeDisplay = new TextView(this);
this.setContentView(timeDisplay);
MyCount counter = new MyCount(30000, 1000);
counter.start();
}
Within an onClick method I have called the method above:
public void onClick(View v) {
setTimer();
}
I'd appreciate any advice on this.