这是我的代码
@Override
public void onClick(View v) {
final AlertDialog.Builder adb = new AlertDialog.Builder(getApplicationContext());
adb.setView(LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom, null));
adb.setPositiveButton("Add",new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);
java.util.Date date = null;
Calendar cal = GregorianCalendar.getInstance();
cal.set(datePicker.getYear(),datePicker.getMonth(), datePicker.getDayOfMonth());
date = cal.getTime();
}
});
adb.show();
}
我在这行中有NullPointerException,我认为datePicker不是findById,因为我使用AlertDialog.Builder。
cal.set(datePicker.getYear(),datePicker.getMonth(),datePicker.getDayOfMonth());
我尝试使用adb.findViewById();
,但这是一个错误(方法findViewById(int)未定义类型AlertDialog.Builder)。
你能帮帮我吗?
答案 0 :(得分:15)
更改
DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);
这一行
DatePicker datePicker = (DatePicker)adb.findViewById(R.id.datePicker);
...............
试试这个:
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.main1, null);
adb.setView(view);
final Dialog dialog;
adb.setPositiveButton("Add",new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
DatePicker datePicker = (DatePicker)view.findViewById(R.id.datePicker1);
java.util.Date date = null;
Calendar cal = GregorianCalendar.getInstance();
cal.set(datePicker.getYear(),datePicker.getMonth(), datePicker.getDayOfMonth());
date = cal.getTime();
}
});
dialog = adb.create();
dialog.show();
答案 1 :(得分:2)
使用DatePicker
参数搜索dialog
:
DatePicker datePicker = (DatePicker) ((Dialog) dialog).findViewById(R.id.datePicker);
答案 2 :(得分:2)
我认为这可能是问题,
final AlertDialog.Builder adb = new AlertDialog.Builder(getApplicationContext());
adb.setView(LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom,null));
使用以下方法为视图充气并使用视图对象获取ID。
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
DatePicker datePicker = (DatePicker)textEntryView.findViewById(R.id.datePicker1);
示例:
protected Dialog onCreateDialog() {
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(TicTacToe.this)
//.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(getTitleText())
.setView(textEntryView)
.setPositiveButton("JOIN GAME", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try
{
EditText et = (EditText)findViewById(R.id.username_edit);
playerName = et.getText().toString();
}
catch (Exception e)
{
}
}
})
.create();
return null;
}
答案 3 :(得分:0)
In my case: First I must call the
dialog.show(),
and only after it I was able to use
dialog.findviewById(R.id.myID).
If I missed to call the show(), than I got a null back with findViewByID.
答案 4 :(得分:0)
项目清理和重建为我修复了这个错误(在Xamarin上)。