Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Unable to get radio button id

Posted on November 11, 2022 By

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;

  1. Rename your activity so that it’s name doesn’t clash with the view’s type
  2. Don’t cast the return value of findViewById(int). It will automatically do that for you based on the type of your variable.
  3. 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" />

READ  [FIXED] android - Custom Listview containing checkedtextview
Powered by Inline Related Posts

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

Android Tags:android, android-radiobutton, java

Post navigation

Previous Post: [FIXED] android – Build not succed in flutter
Next Post: [FIXED] kotlin – Opening the application from the results google.com, always directs to the home page of WebView Android

Related Posts

[FIXED] Samsung Galaxy 4 classic bezel rotation detection in Android Studio (Java) Android
[FIXED] android – How do i put online database into local database Android
[FIXED] xamarin.android – Xamarin.Forms save user in Shared Functions Android
[FIXED] build.gradle – After upgrading android build:gradle:4.0.0 shows me error in proguard Android
[FIXED] How to analyze my own transmitted Bluetooth signal on Android Android
[FIXED] android – Managing a device for public usage and forbidding specific app usage Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme