I have some code in which i use radio Button for selection purpose.but i get en error while click on button. can anyone help me to solve this. My code is as below...
Error Code
2020-03-13 07:35:36.577 9611-9611/com.hpcreation.hanumanchalisa E/id: -12020-03-13 07:35:36.585 9611-9611/com.hpcreation.hanumanchalisa E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hpcreation.hanumanchalisa, PID: 9611
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference
at com.hpcreation.hanumanchalisa.LanguageChooserActivity.startChalisa(LanguageChooserActivity.java:40)
at com.hpcreation.hanumanchalisa.LanguageChooserActivity$1.onClick(LanguageChooserActivity.java:31)
at android.view.View.performClick(View.java:6300)
at android.view.View$PerformClick.run(View.java:24941)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
My Button Click Event
startRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startChalisa();
}});
StartChalisa Method is as :
public void startChalisa() {
int selectedID = group.getCheckedRadioButtonId();
Log.e("id", String.valueOf(selectedID));
RadioButton radioButton = findViewById(selectedID);
if (radioButton.getText() == null) {
Toast.makeText(this, "selectLanguageFirst", Toast.LENGTH_SHORT).show();
} else {
switch (radioButton.getText().toString()) {
case "Hindi":
languageOP(this, new MainActivity(), "Hindi");
Toast.makeText(this, "Hindi", Toast.LENGTH_SHORT).show();
break;
case "English":
languageOP(this, new MainActivity(), "English");
Toast.makeText(this, "English", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}}
LangugeOP method is as:
void languageOP(Context context, MainActivity mainActivity, String choice) {
Intent intent = new Intent(context, mainActivity.getClass()).putExtra("choice", choice);
startActivity(intent);}
I suspect that the problem is here:
int selectedID = group.getCheckedRadioButtonId(); RadioButton radioButton = findViewById(selectedID); if (radioButton.getText() == null) { // ... }
It looks like you're trying to handle the case where no radio button has yet been selected, but not going about it the right way.
getCheckedRadioButtonId()
returns -1
when nothing has been selected. So you could check that immediately:
int selectedID = group.getCheckedRadioButtonId();
if (selectedID == -1) {
// ...
}
Or, if you want to work with the view specifically, you could check if the view is null (not if its text is null):
int selectedID = group.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(selectedID);
if (radioButton == null) {
// ...
}
我认为你应该尝试((RadioButton) group.getChildAt(checkedId))
来找到你的 RadioButton
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.