创建构建器的代码:
builder = new AlertDialog.Builder(this);
builder.setPositiveButton("connect",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
devices.get(currentPos).setConnected(true);
}
});
builder.setNegativeButton("dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.setView(getLayoutInflater().inflate(
(R.layout.activity_device_details), null));
builder.setTitle("more information");
请注意:builder.setView() 到 R.layout.activity_device_details,其中有一些我想在使用以下代码创建对话框时填充的 TextView:
BPDevice dev = new BPDevice();
dev = devices.get(position);
AlertDialog dialog = builder.create();
((TextView) dialog.findViewById(R.id.name)).setText(dev.getName());
dialog.show();
由于这一行,我得到了一个 nullpointerException: ((TextView) dialog.findViewById(R.id.name)).setText(dev.getName());
¿ 如何正确填充 TextViews?