2013-09-11 39 views
0

我想在Android应用程序中实现CountDownTimer。该计时器在运行时将从一个值开始倒计时,而不是从一个不同的值开始倒计时。在数值之间切换回来并强制设置,直到设定的循环数已经过去或按下停止按钮。我可以使CountDownTimer示例工作,但我想我在这里错过了一些东西。以下是适用的按钮代码;CountDownTimer Android问题

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; 
} 

一切都启动好了,但当我点击任一按钮时崩溃。如果我将我的电话注释到workoutTimer,则不会崩溃。我从来没有看到我的日志在workoutTimer类的构造函数中,所以显然我在这里错过了一些东西。任何帮助,将不胜感激。

-Ian

回答

0

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

workoutTimer = new WorkoutTimer(...); 
+0

这种工作,至少我现在得到一个新的错误。 “获取资源编号0x000的值时没有包标识符”似乎在我的setText调用中。 – iYeager

+0

伊恩,看来我回答了这个问题,对吧?你能接受它还是至少标记它有用吗?你的新错误实际上是一个不同的问题,需要审查你的资源文件。谢谢。 –

+0

是的,在调用它之前,上面是问题和初始化。 – iYeager