Android countdown timer not working if i change values

不羁岁月 提交于 2019-12-11 15:55:27

问题


I am trying to make a program that you enter a number with numberpicker on the first activity and on the second activity there is a countdown timer that uses the number you entered on the first activity to countdown from in minutes(if you enter 10 it countdown 10 minutes).

public class timer_2 extends AppCompatActivity {
ImageButton imageButton3;
private TextView timer_2_up;
private TextView timer_2_down;
private CountDownTimer timer_2_up_countdowntimer;
private CountDownTimer timer_2_down_countdowntimer;
private boolean timer_2_up_running;
private boolean timer_2_down_running;
private long starttime;
private long starttimedown = starttime*60000;
private long starttimeup = starttime*60000;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer_2);
    timer_2_up = findViewById(R.id.timer_2_up);
    timer_2_down = findViewById(R.id.timer_2_down);
    imageButton3 = (ImageButton)findViewById(R.id.imageButton3);
    Bundle timer2extras = getIntent().getExtras();
    String timer2string = timer2extras.getString("timer2string");
    starttime = Integer.parseInt(timer2string);
    imageButton3.setOnClickListener(new View.OnClickListener() {
      //....                  

If i remove starttime*60000(9th row) both on startimeup and starttimedown and replace with a number (for example 600000) it works fine but with starttime it just shows 00:00.Please help me.


回答1:


Just declare starttimeup and starttime down in starting and initialise them after you get starttime value from 1st activity like:

long starttimeup;
long starttimedown;

And in onCreate:

starttime = Integer.parseInt(timer2string);
starttimeup = starttime*60000;
starttimedown = starttime*60000;


来源:https://stackoverflow.com/questions/48752345/android-countdown-timer-not-working-if-i-change-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!