我有3个活动:
- 您设置倒数计时器启动的数字 的计时器设置
- 计时器本身
- 按下后面按钮时被激活
我的问题是,在按下"简历"按钮时,在暂停计时器活动(第三个(中,应用程序显示空错误,但是按下主菜单按钮时,应用程序正常工作。
第一次活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_settings);
final 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() + "");
testintent.putExtras(timer2extras);
startActivity(testintent);
}
});
}
第二个活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_2);
//...
Bundle timer2extras = getIntent().getExtras();
String timer2string = timer2extras.getString("timer2string");
starttime = Integer.parseInt(timer2string);
starttimeup = starttime*60000;
starttimedown = starttime*60000;
int minutes_up_start = (int) (starttimeup / 1000) / 60;
int seconds_up_start = (int) (starttimeup / 1000) % 60;
String time_2_up_start_formatted =
String.format(Locale.getDefault(),"%02d:%02d", minutes_up_start,
seconds_up_start);
timer_2_up.setText(time_2_up_start_formatted);
int minutes_down_start = (int) (starttimedown / 1000) / 60;
int seconds_down_start = (int) (starttimedown / 1000) % 60;
String time_2_down_start_formatted =
String.format(Locale.getDefault(),"%02d:%02d", minutes_down_start,
seconds_down_start);
timer_2_down.setText(time_2_down_start_formatted);
//...
@Override
public void onBackPressed()
{
startActivity(new Intent(timer_2.this, timer_2_pause.class));
}
第三活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_2_pause);
this.setFinishOnTouchOutside(false);
Button main = (Button)findViewById(R.id.button8);
Button resu = (Button)findViewById(R.id.button7);
main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mainintent = new Intent(timer_2_pause.this, main_menu.class);
startActivity(mainintent);
}
});
resu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent resuintent = new Intent(timer_2_pause.this, timer_2.class);
startActivity(resuintent);
}
});
}
您的活动两个期望从意图中获取一些数据。
Bundle timer2extras = getIntent().getExtras();
String timer2string = timer2extras.getString("timer2string");
您的timer2extras
为null,当您尝试从中获取字符串时,它会导致Null指针异常。
通过更改简历来修复它:
resu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});