我试图做到这一点,以便当按下按钮时,此方法会启动一个新活动,但应用程序在第二个活动开始之前崩溃。该按钮与方法正确对应,并且新活动在清单文件中列出。
MainActivity 方法:
public void okay(View view) {
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
boolean dayofweek, season, daystill;
dayofweek = ((CheckBox) findViewById(R.id.checkBoxDayofweek)).isChecked();
season = ((CheckBox) findViewById(R.id.checkBoxSeason)).isChecked();
daystill = ((CheckBox) findViewById(R.id.checkBoxDaystill)).isChecked();
intent.putExtra(EXTRA_DAYOFWEEK, dayofweek);
intent.putExtra(EXTRA_SEASON, season);
intent.putExtra(EXTRA_DAYSTILL, daystill);
DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker);
int day, month, year;
day = datepicker.getDayOfMonth();
month = datepicker.getMonth();
year = datepicker.getYear();
intent.putExtra(EXTRA_DAY, day);
intent.putExtra(EXTRA_MONTH, month);
intent.putExtra(EXTRA_YEAR, year);
startActivity(intent);
}
显示活动:
public class DisplayActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
boolean dayofweek = intent.getBooleanExtra(MainActivity.EXTRA_DAYOFWEEK, false);
boolean season = intent.getBooleanExtra(MainActivity.EXTRA_SEASON, false);
boolean daystill = intent.getBooleanExtra(MainActivity.EXTRA_DAYSTILL, false);
int day = intent.getIntExtra(MainActivity.EXTRA_DAY, 0);
int month = intent.getIntExtra(MainActivity.EXTRA_MONTH, 0);
int year = intent.getIntExtra(MainActivity.EXTRA_YEAR, 0);
TextView output = new TextView(this);
output.setTextSize(30);
String s = day + "/" + month + "/" + year + "\n";
if(dayofweek) s += "";
output.setText(s);
setContentView(output);
}
}