将从日期选择器选择的日期传递给另一个 Activity?
Passing date picked from date picker to another Activity?
我在将日期发送给另一个 activity 时遇到问题。出于某种原因,新 Intent 不接受 Datepicker.this
并给我一个 this is not an enclosing class 错误。任何帮助,将不胜感激。
编辑:原来的问题已经解决,但现在当我尝试传递应用程序崩溃的日期时。
public class Setup extends Activity {
private Button btnReport;
private AlertDialog.Builder dialogBuilder;
private String strWeekNumber = "strWeekNumber";
private Button openAppButton;
private TextView report_date;
int year_x, month_x, day_x;
static final int DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
final Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);
setupOpenAppButton();
showDialogOnButtonClick();
}
public void showDialogOnButtonClick() {
btnReport = (Button) findViewById(R.id.btnReport);
btnReport.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog(DIALOG_ID);
}
}
);
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_ID)
return new DatePickerDialog(this, dpickerListner, year_x, month_x, day_x);
return null;
}
private DatePickerDialog.OnDateSetListener dpickerListner
= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x = year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
Intent o = new Intent(Setup.this, UnitsForm.class);
o.putExtra("Date", year_x +"/"+month_x + "/"+ day_x + "/");
startActivity(o);
}
};
单位形式Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle dateData = getIntent().getExtras();
if (dateData == null) {
return;
}
String dateSet = dateData.getString("Date");
final TextView reportDate = (TextView)findViewById(R.id.report_date);
reportDate.setText(dateSet);
您应该将 Intent 传递给您的实际上下文,在您的情况下它将是
Intent intent = new Intent(Setup.this, UnitsForm.class);
而不是
Intent o = new Intent(DatePicker.this, UnitsForm.class);
你的 UnitsForm
activity 的问题是你需要在 final TextView reportDate = (TextView)findViewById(R.id.report_date);
之前调用 setContentView(R.layout.layout_for_UnitsForm);
否则你的 TextView
是 null
。
试一试
Intent o = new Intent(Setup.this, UnitsForm.class);
o.putExtra("Date", year_x +"/"+month_x + "/"+ day_x + "/");
startActivity(o);
我在将日期发送给另一个 activity 时遇到问题。出于某种原因,新 Intent 不接受 Datepicker.this
并给我一个 this is not an enclosing class 错误。任何帮助,将不胜感激。
编辑:原来的问题已经解决,但现在当我尝试传递应用程序崩溃的日期时。
public class Setup extends Activity {
private Button btnReport;
private AlertDialog.Builder dialogBuilder;
private String strWeekNumber = "strWeekNumber";
private Button openAppButton;
private TextView report_date;
int year_x, month_x, day_x;
static final int DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
final Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);
setupOpenAppButton();
showDialogOnButtonClick();
}
public void showDialogOnButtonClick() {
btnReport = (Button) findViewById(R.id.btnReport);
btnReport.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog(DIALOG_ID);
}
}
);
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_ID)
return new DatePickerDialog(this, dpickerListner, year_x, month_x, day_x);
return null;
}
private DatePickerDialog.OnDateSetListener dpickerListner
= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x = year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
Intent o = new Intent(Setup.this, UnitsForm.class);
o.putExtra("Date", year_x +"/"+month_x + "/"+ day_x + "/");
startActivity(o);
}
};
单位形式Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle dateData = getIntent().getExtras();
if (dateData == null) {
return;
}
String dateSet = dateData.getString("Date");
final TextView reportDate = (TextView)findViewById(R.id.report_date);
reportDate.setText(dateSet);
您应该将 Intent 传递给您的实际上下文,在您的情况下它将是
Intent intent = new Intent(Setup.this, UnitsForm.class);
而不是
Intent o = new Intent(DatePicker.this, UnitsForm.class);
你的 UnitsForm
activity 的问题是你需要在 final TextView reportDate = (TextView)findViewById(R.id.report_date);
之前调用 setContentView(R.layout.layout_for_UnitsForm);
否则你的 TextView
是 null
。
试一试
Intent o = new Intent(Setup.this, UnitsForm.class);
o.putExtra("Date", year_x +"/"+month_x + "/"+ day_x + "/");
startActivity(o);