I'm creating a dialog like this:
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);
...
And after that I want to set the OnCheckedChangeListener:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
...
}
});
But whenever I try to show the dialog, it crashes my app. The log says:
Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference
So I'm guessing the radiogroup isn't created yet when I'm trying to set the listener. But how do I set it then?
Try replacing
RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
with
RadioGroup rg = (RadioGroup) promptView.findViewById(R.id.rg);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加