我想从另一个xml文件中的DatePicker获取日期(日,月,年)。但是app错误NullPointerException没有使用theInflatedView。如果我使用InflatedView,则不会获得日期。我如何从另一个xml文件中的DatePicker获取日期?
public void customAlertDialog(){
final Dialog dialog = new Dialog(HesapActivity.this);
dialog.setContentView(R.layout.dateset);
dialog.setTitle("Date");
theInflatedView = getLayoutInflater().inflate(R.layout.dateset, null);
final DatePicker dpFirst = (DatePicker) theInflatedView.findViewById(R.id.datePicker1);
final str;
dpFirst.init(dpFirst.getYear(), dpFirst.getMonth(), dpFirst.getDayOfMonth(),new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
str=(arg3+ "."+ (arg2+1) + "."+arg1);
}
} );
Button dialogButton = (Button) dialog.findViewById(R.id.setDPBt);
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textFirst.setText(str);//textFirst is MainActivity variable.
dialog.dismiss();
}
});
dialog.show();
}
onCreate()中的
customAlertDialog();
单击AlertDialog中的按钮
时,不要设置文本日期------ --------更新
我在下面解决了问题
final DatePicker dpFirst = (DatePicker) dialog.findViewById(R.id.datePicker1);//I changed theInflatedView to dialog
答案 0 :(得分:1)
以下更改:
public void customAlertDialog(){
final Dialog dialog = new Dialog(HesapActivity.this);
dialog.setContentView(R.layout.dateset);
dialog.setTitle("Date");
theInflatedView = getLayoutInflater().inflate(R.layout.dateset, null);
final DatePicker dpFirst = (DatePicker) theInflatedView.findViewById(R.id.datePicker1);
final str;
dpFirst.init(dpFirst.getYear(), dpFirst.getMonth(), dpFirst.getDayOfMonth(),new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
str=(arg3+ "."+ (arg2+1) + "."+arg1);
}
} );
Button dialogButton = (Button) dialog.findViewById(R.id.setDPBt);
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textFirst.setText(str);//textFirst is MainActivity variable.
dialog.dismiss();
}
});
dialog.show();
}
要
public void customAlertDialog(){
final Dialog dialog = new Dialog(HesapActivity.this);
dialog.setContentView(R.layout.dateset);
dialog.setTitle("Date");
theInflatedView = getLayoutInflater().inflate(R.layout.dateset, null);
final DatePicker dpFirst = (DatePicker) theInflatedView.findViewById(R.id.datePicker1);
final str;
dpFirst.init(dpFirst.getYear(), dpFirst.getMonth(), dpFirst.getDayOfMonth(),new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
textFirst.setText(arg3+ "."+ (arg2+1) + "."+arg1);
}
} );
Button dialogButton = (Button) dialog.findViewById(R.id.setDPBt);
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}