Solution 1 :
According to the exception message you’re getting it appears to be an issue with casting the view returned by findViewById(int)
. Try the following steps below;
- Rename your activity so that it’s name doesn’t clash with the view’s type
- Don’t cast the return value of
findViewById(int)
. It will automatically do that for you based on the type of your variable. - Make sure you have the correct imports.
After taking the above steps the resulting activity should be like below
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton; // this is the important part
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
// activity name changed!
public class RadioButtonActivity extends AppCompatActivity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioSexGroup = findViewById(R.id.radioSex);
btnDisplay = findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = findViewById(selectedId);
// find the radiobutton by returned id
Toast.makeText(getApplicationContext(),
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
You’re encountering the issue because you’re attempting to cast the return value of findViewById(int)
into “RadioButton” which in your code is referring to the activity instead of android.widget.RadioButton
Solution 2 :
change your code to below code
private View radioSexButton; //make View
int radioButtonID = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = radioSexGroup.findViewById(radioButtonID);
Problem :
I am trying to get radio button value on click of a button but it is showing red line error below this line:
I want to know why this error is showing as I am unable to understand.
radioSexButton = (RadioButton)findViewById(selectedId);
inconvertible types,cannot cast android.view.view to RadioButton
Below is my code:
XML code:
<LinearLayout
xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="match_parent"
tools_context=".RadioButton"
android_padding="20dp">
<RadioGroup
android_id="@+id/radioSex"
android_layout_width="wrap_content"
android_layout_height="wrap_content" >
<RadioButton
android_id="@+id/radioMale"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Male"
android_checked="true" />
<RadioButton
android_id="@+id/radioFemale"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Female" />
</RadioGroup>
<Button
android_id="@+id/btnDisplay"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Display" />
JAVA code:
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
public class RadioButton extends AppCompatActivity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);
radioSexGroup = findViewById(R.id.radioSex);
btnDisplay = findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton)findViewById(selectedId);
// find the radiobutton by returned id
Toast.makeText(getApplicationContext(),
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
Someone please let me know how to resolve this issue any help would be appreciated.
THANKS
Comments
Comment posted by Subaru Tashiro
make sure to include your imports in your sample code next time 😉
Comment posted by Digvijay
I am adding imports just updating
Comment posted by Subaru Tashiro
it’s okay, i’ve posted an answer. The issue is regarding imports and your activity name.
Comment posted by Digvijay
added imports in my post plase look at my imports
Comment posted by Subaru Tashiro
Yes, I can confirm that the missing import for
Comment posted by Subaru Tashiro
this won’t work because you can’t call
Comment posted by Digvijay
If I change radioSexButton to View then in Toast radioSexButton.getText() is showing error