我正在创建一个这样的对话框:
private void MyDialogFragment() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.mydialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(promptView);
final RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
...
之后我想设置OnCheckedChangeListener:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
...
}
});
但每当我尝试显示对话框时,它都会崩溃我的应用。日志说:
Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference
所以当我试图设置监听器时,我猜测还没有创建radiogroup。但是我如何设置呢?
答案 0 :(得分:0)
尝试替换
RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
与
RadioGroup rg = (RadioGroup) promptView.findViewById(R.id.rg);