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" />
</LinearLayout>
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();
}
});
}
}
How can I resolve this issue?
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;
findViewById(int)
. It will automatically do that for you based on the type of your variable.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