Java 无法获取单选按钮id

Java 无法获取单选按钮id,java,android,android-radiobutton,Java,Android,Android Radiobutton,我试图在单击按钮时获取单选按钮值,但在此行下方显示红线错误: 我想知道为什么会出现这个错误,因为我无法理解 radioSexButton = (RadioButton)findViewById(selectedId); inconvertible types,cannot cast android.view.view to RadioButton 下面是我的代码: XML代码: <LinearLayout xmlns:android="http://schemas.android.co

我试图在单击按钮时获取单选按钮值,但在此行下方显示红线错误:

我想知道为什么会出现这个错误,因为我无法理解

radioSexButton = (RadioButton)findViewById(selectedId);

inconvertible types,cannot cast android.view.view to RadioButton
下面是我的代码:

XML代码:

<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" />
有人请让我知道如何解决这个问题,任何帮助将不胜感激


谢谢

将代码更改为下面的代码

private View radioSexButton; //make View
int radioButtonID = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = radioSexGroup.findViewById(radioButtonID);

根据您收到的异常消息,强制转换由
findViewById(int)
返回的视图似乎有问题。尝试以下步骤

  • 重命名活动,使其名称与视图的类型不冲突
  • 不要强制转换
    findViewById(int)
    的返回值。它将根据变量的类型自动为您执行此操作
  • 确保您有正确的导入
  • 采取上述步骤后,产生的活动应如下所示

    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();
                }
            });
        }
    }
    

    您遇到此问题是因为您试图将
    findViewById(int)
    的返回值强制转换为“RadioButton”,它在您的代码中指的是活动,而不是
    android.widget.RadioButton

    ,因为您无法调用
    getText()
    在视图上如果我将RadioExButton更改为View,则在Toast RadioExButton中。getText()显示错误请确保下次在示例代码中包含导入;)我正在添加导入只是更新。没关系,我已经发布了一个答案。问题是关于导入和您的活动名称。在我的post plase中添加了导入查看我的导入是的,我可以确认
    android.widget.RadioButton
    的缺少导入与您的活动名称一起是问题所在
    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();
                }
            });
        }
    }