1

In the text view, I'll put a variable value of the string as text. The error I'm facing is: FATAL EXCEPTION: main Process: app.com.application, PID: 14336 Java. Lang. ClassCastException: Java. Lang. String cannot cast to Java. Lang. Integer atapp.com.application.activity.PayActivity$1.onCheckedChanged(PayActivity.java:41)

Also, this is my code for this part:

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

            }
        });
    }


}

Also, tags include "0" ,"1", ,"2" and "3". Can you guide me where is my problem? Thanks

Haj Ali
  • 93
  • 2
  • 11

1 Answers1

6

You can't cast a string containing an integer value to an integer value:

(int) radioButton.getTag()

You have to explicitly convert to a string (since getTag() returns an Object), then parse:

Integer.parseInt(radioButton.getTag().toString())
Andy Turner
  • 137,514
  • 11
  • 162
  • 243