这部分代码用于创建自定义对话框..
final Dialog dialog = new Dialog(preferences.this);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("Title...");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Bonboniziraj se i ti :)");
Button button = (Button) dialog.findViewById(R.id.dialogButtonOK);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
dialog.show();
}
});
我收到SetOnClickListener错误!!
答案 0 :(得分:4)
final Dialog dialog = new Dialog(preferences.this);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.customdialog, null);
dialog.setContentView(layout);
dialog.setTitle("Title...");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Bonboniziraj se i ti :)");
Button button = (Button) dialog.findViewById(R.id.dialogButtonOK);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
dialog.show();
}
});
答案 1 :(得分:0)
Null指针异常吗?你确定你在customdialog布局中有id = dialogButtonOK的按钮吗?检查一下。