RadioGroup 检索为空
RadioGroup retrieve is null
我尝试检索我的 RADIOGROUP 以检查选中了哪个单选按钮。为此,我在 onCreate() 中使用此代码:
/**
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
radioGroup_LANGUE = (RadioGroup) findViewById(R.id.RadioGroup_LANGUE);
radioGroup_MODE = (RadioGroup) findViewById(R.id.RGroup_ModeConnexion);
... }
但是我用debug,AndroidStudio告诉我radioGroup_LANGUE是null。所以我得到 NULLPOINTER EXCEPTION.
在我的警报对话框中,当用户单击“确定”按钮时:
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
radioGroup_LANGUE.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radioGroup_LANGUE, int checkedId) {
// This will get the radiobutton that has changed in its check state
RadioButton checkedRadioButton = (RadioButton)radioGroup_LANGUE.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)
{
// Changes the textview's text to "Checked: example radiobutton text"
Toast.makeText(MainActivity.this,"Checked:" + checkedRadioButton.getText(),Toast.LENGTH_LONG).show();
}
}
});
}});
我的xml:
<RadioGroup
android:layout_width="148dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/RadioGroup_LANGUE">
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="@string/Langue_1"
android:id="@+id/BouttonRADIO_EN"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="@string/Langue_2"
android:id="@+id/BouttonRADIO_FR"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
</RadioGroup>
错误日志:
06-01 22:51:53.053 21336-21336/com.example.my020571.sterela2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.my020571.sterela2, PID: 21336
java.lang.NullPointerException
at com.example.my020571.sterela2.MainActivity.onClick(MainActivity.java:321)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:170)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5598)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
第 321 行:
int selectedID = rGroup_LANGUE.getCheckedRadioButtonId();
方法:
private void changerLangue() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
final View view = View.inflate(MainActivity.this, R.layout.changer_langue, null);
alertDialogBuilder.setView(view);
// Titre de la fenêtre
alertDialogBuilder.setTitle("Langue");
// set dialog message
alertDialogBuilder
.setMessage("Veuillez choisir votre langue :")
.setCancelable(false)
.setIcon(R.drawable.logo_langue)
.setPositiveButton("ANNULER", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
})
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
int selectedID = rGroup_LANGUE.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton)findViewById(selectedID);
Toast.makeText(getApplicationContext(),selectedRadioButton.getText().toString(),Toast.LENGTH_SHORT).show();
}});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
如果 onCreate
中没有 setContentView
,您的所有视图都将为空。
private void RadioGroup radioGroup_LANGUE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayoutFile); // TODO: Replace with your file
radioGroup_LANGUE = (RadioGroup) findViewById(R.id.RadioGroup_LANGUE);
}
它们为空的另一个原因是,如果您不使用 setContentView
中包含 @+id/RadioGroup_LANGUE
(以及您想要查找的其他 ID)的 XML 文件。
编辑
由于您使用 AlertDialog 来显示视图,因此您需要在展开视图上使用 findViewById
而不是 Activity。
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
// Get the view for the dialog
final View view = View.inflate(MainActivity.this, R.layout.changer_langue, null);
// Find views within it
rGroup_LANGUE = (RadioGroup) view.findViewById(R.id.RadioGroup_LANGUE);
rGroup_LANGUE.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId);
// TODO: Implement this
}});
// build the dialog
AlertDialog alertDialog = alertDialogBuilder
.setView(view) // load the view
.setTitle("Langue")
.setMessage("Veuillez choisir votre langue")
...
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Need to use findViewById from the RadioGroup here
int selectedID = rGroup_LANGUE.getCheckedRadioButtonId();
String msg = null;
if (selectedId != -1) {
RadioButton selectedRadioButton = (RadioButton)rGroup_LANGUE.findViewById(selectedID);
msg = selectedRadioButton.getText().toString();
} else {
msg = "Nothing selected";
}
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
}})
.create();
// show it
alertDialog.show();
旁注:我认为 setMessage
和 setView
不能同时使用。
使用自定义对话框尝试此代码:
public void changerLangue(){
final Dialog mDialog;
mDialog = new Dialog(MainActivity.this);
mDialog.setContentView(R.layout.changer_langue);
Button ok = (Button)mDialog.findViewById(R.id.btn_ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup yourRadioGroup=(RadioGroup)mDialog.findViewById(R.id.RadioGroup_LANGUE);
int selectedID = yourRadioGroup.getCheckedRadioButtonId();
selectedRadioButton = (RadioButton)mDialog.findViewById(selectedID);
Toast.makeText(getApplicationContext(),selectedRadioButton.getText().toString(),Toast.LENGTH_SHORT).show();
});
Button dismiss = (Button)mDialog.findViewById(R.id.btn_dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.show();
}
}
在您的 changer_langue xml 中添加两个按钮 btn_ok 和 btn_dismiss。它就像魅力一样!干杯
我尝试检索我的 RADIOGROUP 以检查选中了哪个单选按钮。为此,我在 onCreate() 中使用此代码:
/**
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
radioGroup_LANGUE = (RadioGroup) findViewById(R.id.RadioGroup_LANGUE);
radioGroup_MODE = (RadioGroup) findViewById(R.id.RGroup_ModeConnexion);
... }
但是我用debug,AndroidStudio告诉我radioGroup_LANGUE是null。所以我得到 NULLPOINTER EXCEPTION.
在我的警报对话框中,当用户单击“确定”按钮时:
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
radioGroup_LANGUE.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radioGroup_LANGUE, int checkedId) {
// This will get the radiobutton that has changed in its check state
RadioButton checkedRadioButton = (RadioButton)radioGroup_LANGUE.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)
{
// Changes the textview's text to "Checked: example radiobutton text"
Toast.makeText(MainActivity.this,"Checked:" + checkedRadioButton.getText(),Toast.LENGTH_LONG).show();
}
}
});
}});
我的xml:
<RadioGroup
android:layout_width="148dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/RadioGroup_LANGUE">
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="@string/Langue_1"
android:id="@+id/BouttonRADIO_EN"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="@string/Langue_2"
android:id="@+id/BouttonRADIO_FR"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
</RadioGroup>
错误日志:
06-01 22:51:53.053 21336-21336/com.example.my020571.sterela2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.my020571.sterela2, PID: 21336
java.lang.NullPointerException
at com.example.my020571.sterela2.MainActivity.onClick(MainActivity.java:321)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:170)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5598)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
第 321 行:
int selectedID = rGroup_LANGUE.getCheckedRadioButtonId();
方法:
private void changerLangue() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
final View view = View.inflate(MainActivity.this, R.layout.changer_langue, null);
alertDialogBuilder.setView(view);
// Titre de la fenêtre
alertDialogBuilder.setTitle("Langue");
// set dialog message
alertDialogBuilder
.setMessage("Veuillez choisir votre langue :")
.setCancelable(false)
.setIcon(R.drawable.logo_langue)
.setPositiveButton("ANNULER", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
})
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
int selectedID = rGroup_LANGUE.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton)findViewById(selectedID);
Toast.makeText(getApplicationContext(),selectedRadioButton.getText().toString(),Toast.LENGTH_SHORT).show();
}});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
如果 onCreate
中没有 setContentView
,您的所有视图都将为空。
private void RadioGroup radioGroup_LANGUE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayoutFile); // TODO: Replace with your file
radioGroup_LANGUE = (RadioGroup) findViewById(R.id.RadioGroup_LANGUE);
}
它们为空的另一个原因是,如果您不使用 setContentView
中包含 @+id/RadioGroup_LANGUE
(以及您想要查找的其他 ID)的 XML 文件。
编辑
由于您使用 AlertDialog 来显示视图,因此您需要在展开视图上使用 findViewById
而不是 Activity。
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
// Get the view for the dialog
final View view = View.inflate(MainActivity.this, R.layout.changer_langue, null);
// Find views within it
rGroup_LANGUE = (RadioGroup) view.findViewById(R.id.RadioGroup_LANGUE);
rGroup_LANGUE.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId);
// TODO: Implement this
}});
// build the dialog
AlertDialog alertDialog = alertDialogBuilder
.setView(view) // load the view
.setTitle("Langue")
.setMessage("Veuillez choisir votre langue")
...
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Need to use findViewById from the RadioGroup here
int selectedID = rGroup_LANGUE.getCheckedRadioButtonId();
String msg = null;
if (selectedId != -1) {
RadioButton selectedRadioButton = (RadioButton)rGroup_LANGUE.findViewById(selectedID);
msg = selectedRadioButton.getText().toString();
} else {
msg = "Nothing selected";
}
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
}})
.create();
// show it
alertDialog.show();
旁注:我认为 setMessage
和 setView
不能同时使用。
使用自定义对话框尝试此代码:
public void changerLangue(){
final Dialog mDialog;
mDialog = new Dialog(MainActivity.this);
mDialog.setContentView(R.layout.changer_langue);
Button ok = (Button)mDialog.findViewById(R.id.btn_ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup yourRadioGroup=(RadioGroup)mDialog.findViewById(R.id.RadioGroup_LANGUE);
int selectedID = yourRadioGroup.getCheckedRadioButtonId();
selectedRadioButton = (RadioButton)mDialog.findViewById(selectedID);
Toast.makeText(getApplicationContext(),selectedRadioButton.getText().toString(),Toast.LENGTH_SHORT).show();
});
Button dismiss = (Button)mDialog.findViewById(R.id.btn_dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.show();
}
}
在您的 changer_langue xml 中添加两个按钮 btn_ok 和 btn_dismiss。它就像魅力一样!干杯