Reputation: 3
Actually I am a beginner to android , I want to show the selected radio button's
text as a toast but I'm unable to get the text and it's showing null point exception error.
My Code is:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_signup, container, false);
show = (Button) v.findViewById(R.id.show);
gender = (RadioGroup) v.findViewById(R.id.gender);
show.setOnClickListener(this);
return v;}
public void onClick(View view) {
int id=gender.getCheckedRadioButtonId();
RadioButton radio=(RadioButton)view.findViewById(id);
String user_gender=radio.getText().toString();
switch (view.getId()){
case R.id.show:
Toast.makeText(getContext(),user_gender,Toast.LENGTH_SHORT);
break;}}
My error in logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: community.infinity, PID: 24245
java.lang.NullPointerException
at layout.fragment.onClick(fragment.java:118)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 1233
Reputation: 854
You can give it a try.
//Returns the identifier of the selected radio button in current group.
int id = rg1.getCheckedRadioButtonId();
if(id !=-1){
//get radio button view
View radioButton = rg1.findViewById(id);
// return index of selected radiobutton
int radioId = radioGroup.indexOfChild(radioButton);
// based on index getObject of radioButton
RadioButton btn = (RadioButton) rg1.getChildAt(radioId);
//After getting radiobutton you can now use all its methods
String selection = (String) btn.getText();
}
Upvotes: 1
Reputation: 3422
You can use below code;
radioGroup = (RadioGroup) findViewById(R.id.radio);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
Upvotes: 0
Reputation: 69734
change this in your code
RadioButton radio=(RadioButton)view.findViewById(id);
to this
RadioButton radio=(RadioButton)v.findViewById(id);
like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_signup, container, false);
show = (Button) v.findViewById(R.id.show);
gender = (RadioGroup) v.findViewById(R.id.gender);
public void onClick(View view) {
int id=gender.getCheckedRadioButtonId();
RadioButton radio=(RadioButton)v.findViewById(id);
String user_gender=radio.getText().toString();
switch (view.getId()){
case R.id.show:
Toast.makeText(getContext(),user_gender,Toast.LENGTH_SHORT);
break;}}
Upvotes: 0