在文本视图中,我将字符串的变量值作为文本.我面临的错误是:致命异常:主进程:app.com.application,PID:14336 Java.郎.ClassCastException:Java.郎.字符串无法强制转换为Java.郎.整数atapp.com.application.activity.PayActivity $ 1.onCheckedChanged(PayActivity.java:41)
另外,这是我的这部分代码:
public class PayActivity extends AppCompatActivity {
Button button;
RadioGroup radioGroup;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay);
button = findViewById(R.id.btnPay);
radioGroup = findViewById(R.id.radioPay);
textView = findViewById(R.id.pay);
final String[] TextShow ={
"???? ??? ???? 2500 ????? ???",
"???? ??? ???? 7000 ????? ???",
"???? ??? ???? 10000 ????? ???",
"???? ??? ???? 20000 ????? ???"
};
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(selectedId);
final int ArticleTag = (int) radioButton.getTag();
textView.setText(TextShow[ArticleTag]);
Toast.makeText(PayActivity.this,TextShow[ArticleTag] , Toast.LENGTH_SHORT).show();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
此外,标签包括"0" ,"1", ,"2" and "3".你能指导我在哪里问题吗?谢谢
您不能将包含整数值的字符串强制转换为整数值:
(int) radioButton.getTag()
Run Code Online (Sandbox Code Playgroud)
你必须显式转换为字符串(因为getTag()返回一个Object),然后解析:
Integer.parseInt(radioButton.getTag().toString())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
339 次 |
| 最近记录: |