我试图通过单击按钮获取单选按钮值,但它在该行下方显示红线错误:
我想知道为什么会出现这个错误,因为我无法理解。
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" />
</LinearLayout>
Java代码:
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();
}
});
}
}
如何解决这个问题?
根据您收到的异常消息,这似乎是转换
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
将您的代码更改为以下代码
private View radioSexButton; //make View
int radioButtonID = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = radioSexGroup.findViewById(radioButtonID);