0

我正在尝试在 Android 应用程序中实现 CountDownTimer。此计时器将在运行时从一个值倒计时,而不是重置,而不是从另一个值倒计时。在值之间切换并强制,直到经过设定的轮数或按下停止按钮。我可以让 CountDownTimer 样本工作,但我想我在这里遗漏了一些东西。以下是适用的按钮按下代码;

CounterState state = CounterState.WORKOUT;
private WorkoutTimer workoutTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workout_stopwatch); 
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Set up OnClickListeners
    ((Button) findViewById(R.id.start_button)).setOnClickListener(this);
    ((Button) findViewById(R.id.stop_button)).setOnClickListener(this);
    ((Button) findViewById(R.id.reset_button)).setOnClickListener(this);

}

@Override
public void onClick(View v) {       
    switch(v.getId()) {
    case R.id.start_button:
        if (!timer_running) {
            timer_running = true;
            Log.d(TAG, "clicked on Start Button");

            // If the state is unknown, set it to Workout first
            int State = state.getStateValue();
            if (State == 0) {
                state.setStateValue(1);
            }
            workoutTimer.start();
        }
        break;
    case R.id.stop_button:
        Log.d(TAG, "clicked on Stop Button");

        if (timer_running); {
            timer_running = false;
            workoutTimer.cancel();
        }

        break;

private class WorkoutTimer extends CountDownTimer{
    public WorkoutTimer(long interval) {
        super(getThisTime(), interval);
        Log.d(TAG, "WorkoutTimer Constructed...");
    }

    TextView digital_display = (TextView) findViewById(R.id.digital_display);
    TextView numOfRounds = (TextView) findViewById(R.id.number_of_rounds);

    public void onFinish() {
        int State = state.getStateValue();
        int roundsLeft = 0;

        if (State == 1) {
            state.setStateValue(2);
        } else {
            state.setStateValue(1);
        }

        decrementRounds();

        try {
            roundsLeft = Integer.parseInt(numOfRounds.getText().toString());
        } catch(NumberFormatException nfe) {
            roundsLeft = 999;
        }

        if (roundsLeft > 0 || roundsLeft != 999) {
            workoutTimer.start();
        }
    }

    public void onTick(long millisUntilFinished) {
        final long minutes_left = ((millisUntilFinished / 1000) / 60);
        final long seconds_left = (millisUntilFinished / 1000) - (minutes_left * 60);
        final long millis_left = millisUntilFinished % 100;
        String time_left = String.format("%02d:%02d.d", minutes_left, seconds_left, 
                millis_left);
        digital_display.setText(time_left);
    }
}

private long getThisTime() {
    long time = 0;

    TextView workout_time = (TextView) findViewById(R.id.workout_time);
    TextView rest_time = (TextView) findViewById(R.id.rest_time);

    switch(state) {
    case WORKOUT:
        try {
                time = Integer.parseInt(workout_time.getText().toString());
            } catch(NumberFormatException nfe) {
                time = 999;
            } 
//          time = 90;
        Log.d(TAG, "Workout time = " + time);
        break;
    case REST:
        try {
                time = Integer.parseInt(rest_time.getText().toString());
            } catch(NumberFormatException nfe) {
                time = 999;
            } 
//          time = 30;
        Log.d(TAG, "Rest time = " + time);
        break;
    case UNKNOWN:
        time = 0;
        break;
    }
    return time;
}

一切正常,但是当我单击任一按钮时崩溃。如果我注释掉我对锻炼计时器的调用,则不会崩溃。我从来没有在锻炼计时器类的构造函数中看到我的日志,所以很明显我在这里遗漏了一些东西。任何帮助,将不胜感激。

-伊恩

4

1 回答 1

0

您尚未初始化您的锻炼计时器。您需要在 onCreate 方法中添加以下行。

workoutTimer = new WorkoutTimer(...);
于 2013-09-11T03:53:07.517 回答