我想检查在我的对话框中选中了我的 radiogroup 的哪个单选按钮。我已经尝试过了(正如您在下面看到的那样),但它在这一行中崩溃了:RadioGroup rGroup = (RadioGroup)getDialog().findViewById(R.id.radioGroup);
出现空指针异常。
这是我的代码:
class newLessonDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.new_lesson_dialog, null))
// Add action buttons,
.setPositiveButton("Speichern", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id)
{
EditText eF = (EditText)getDialog().findViewById(R.id.editFach);
fach = eF.getText().toString();
EditText eR = (EditText)getDialog().findViewById(R.id.editRaum);
raum = eR.getText().toString();
EditText eL = (EditText)getDialog().findViewById(R.id.editLehrer);
lehrer = eL.getText().toString();
if (tag != null)
{
save(fach, raum, lehrer, tag, index);
}
else
{
Toast.makeText(getAppContext(), "Zuerst Wochentag auswählen!", Toast.LENGTH_SHORT);
}
}
})
.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
newLessonDialog.this.getDialog().cancel();
}
});
// This will get the radiogroup
RadioGroup rGroup = (RadioGroup)getDialog().findViewById(R.id.radioGroup);
// This will get the radiobutton in the radiogroup that is checked
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
{
// This will get the radiobutton that has changed in its check state
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
// This puts the value (true/false) into the variable
boolean isChecked = checkedRadioButton.isChecked();
// If the radiobutton that has changed in check state is now checked...
if (isChecked)
{
tag = checkedRadioButton.getText().toString();
}
}
});
return builder.create();
}
}
这是我的日志:
08-14 16:16:23.749 7618-7618/de.nathan.android.droidschool D/AndroidRuntime: Shutting down VM
08-14 16:16:23.749 7618-7618/de.nathan.android.droidschool W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41760700)
08-14 16:16:23.779 7618-7618/de.nathan.android.droidschool E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at de.nathan.android.droidschool.MainActivity$fragmentTab1$1$1newLessonDialog.onCreateDialog(MainActivity.java:303)
at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
08-14 16:16:23.789 438-665/? W/ActivityManager: Force finishing activity de.nathan.android.droidschool/.MainActivity
08-14 16:16:24.309 438-454/? W/ActivityManager: Activity pause timeout for ActivityRecord{41ef07a8 u0 de.nathan.android.droidschool/.MainActivity}
08-14 16:16:34.899 438-454/? W/ActivityManager: Activity destroy timeout for ActivityRecord{41ef07a8 u0 de.nathan.android.droidschool/.MainActivity}