On the first activity I want to set the number with a number picker and I have this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_settings);
NumberPicker np = (NumberPicker)findViewById(R.id.numberPicker2);
np.setMinValue(1);
np.setMaxValue(999);
np.setWrapSelectorWheel(false);
np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Intent timer2 = new Intent(timer_settings.this, timer_2.class);
Bundle timer2extras = new Bundle();
timer2extras.putString("timer2string", newVal + "");
timer2.putExtras(timer2extras);
}
});
final Button testbutton = (Button) findViewById(R.id.testbutton);
testbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent testintent = new Intent(timer_settings.this, timer_2.class);
startActivity(testintent);
}
});
}
And on the second activity I want to use the number as the start time for the countdown, and I have this:
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 Bundle timer2extras = getIntent().getExtras();
private String timer2string = timer2extras.getString("timer2string");
private long starttime = Integer.parseInt(timer2string);
@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);
imageButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageButton3.animate().rotation(imageButton3.getRotation()+180).start();
imageButton3.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
imageButton3.setEnabled(true);
}
});
}
}, 300);
if (timer_2_up_running){
pausetimer_2_up();
starttimer_2_down();
}else {
starttimer_2_up();
if (timer_2_down_running) {
pausetimer_2_down();
}
}
}
});
}
private void starttimer_2_up() {
timer_2_up_countdowntimer = new CountDownTimer(starttime, 1000) {
@Override
public void onTick(long millisUntilFinished) {
starttime = millisUntilFinished;
update_up_text();
}
@Override
public void onFinish() {
timer_2_up_running=false;
}
}.start();
timer_2_up_running = true;
}
private void starttimer_2_down() {
timer_2_down_countdowntimer = new CountDownTimer(starttime, 1000) {
@Override
public void onTick(long millisUntilFinished) {
starttime = millisUntilFinished;
update_down_text();
}
@Override
public void onFinish() {
timer_2_down_running=false;
}
}.start();
timer_2_down_running=true;
}
private void pausetimer_2_up () {
timer_2_up_countdowntimer.cancel();
timer_2_up_running=false;
}
private void pausetimer_2_down() {
timer_2_down_countdowntimer.cancel();
timer_2_down_running=false;
}
private void update_up_text() {
int minutes_up = (int) (starttime / 1000) / 60;
int seconds_up = (int) (starttime / 1000) % 60;
String time_2_up_left_formatted = String.format(Locale.getDefault(),"%02d:%02d", minutes_up, seconds_up);
timer_2_up.setText(time_2_up_left_formatted);
}
private void update_down_text() {
int minutes_down = (int) (starttime / 1000) / 60;
int seconds_down = (int) (starttime / 1000) % 60;
String time_2_down_left_formatted = String.format(Locale.getDefault(),"%02d:%02d", minutes_down, seconds_down);
timer_2_down.setText(time_2_down_left_formatted);
}
But it does not work showing null errors that I cant find a solution to. I don't know if this is because of the long/int difference between the NumberPicker and the countdown timer or I forgot to add some line.
Please help me.
Because you are put bundle extra to a Intent, then start another Intent. You don't have to watch for NumberPicker
's value changed event. Just getValue()
whenever you want to start timer activity.
In your first activity, it should be
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_settings);
NumberPicker np = (NumberPicker)findViewById(R.id.numberPicker2);
np.setMinValue(1);
np.setMaxValue(999);
np.setWrapSelectorWheel(false);
final Button testbutton = (Button) findViewById(R.id.testbutton);
testbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent testintent = new Intent(timer_settings.this, timer_2.class);
Bundle timer2extras = new Bundle();
timer2extras.putString("timer2string", np.getValue() + "");
timer2.putExtras(timer2extras);
startActivity(testintent);
}
});
}
In the timer activity, put the code that getIntent().getExtras()
inside onCreate()
when everything is ready.
private Bundle timer2extras;
private String timer2string;
private long starttime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_2);
// Find views
timer2extras = getIntent().getExtras();
timer2string = timer2extras.getString("timer2string");
starttime = Integer.parseInt(timer2string);
// ...
}