0

Am I doing this right? I'm trying to implement a simple countdown timer just to get my feet of the ground with it.

my timer class is not nested in my activity, and all by itself in it's own java file, for the sake of neatness. here:

public class McatTimer extends CountDownTimer {

    private TextView timer_tv;

    public McatTimer(long millisInFuture, long countDownInterval, TextView textview) {
        super(millisInFuture, countDownInterval);
        this.timer_tv = textview;
    }

    @Override   
    public void onFinish() {
        timer_tv.setText("DONE!");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        timer_tv.setText("seconds remaining: " + millisUntilFinished / 1000);
    }

}

and here is logcat:

10-03 08:40:36.007: E/AndroidRuntime(2582): FATAL EXCEPTION: main
10-03 08:40:36.007: E/AndroidRuntime(2582): java.lang.NullPointerException
10-03 08:40:36.007: E/AndroidRuntime(2582):     at com.mangodeveloper.mcathomie.McatTimer.onTick(McatTimer.java:22)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at android.os.Looper.loop(Looper.java:123)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at java.lang.reflect.Method.invoke(Method.java:507)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at dalvik.system.NativeStart.main(Native Method)

and my activity has these in their appropriate places:

private McatTimer mcatTimer;

....

mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);
mcatTimer.start();

Also is there some trick to reading these logcats? I'm sorry but i'm self-teaching.

4

1 回答 1

1

Looks like your TextView object is null. Please ensure that you have intialized your TextView before you call the

mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);

Call this,

timerTv=(TextView)findViewById(R.id.textview);
于 2012-10-03T08:50:36.300 回答