I was implementing the following Java code in Android Studio:
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(number);
...
}
This is a part of a larger application.
As you can see, I've passed only an integer value to the quantityTextView.setText(number)
method.
When running the app, it crashes as soon as this method is called. Can you tell me why such a thing is happening?
Yes, use String.valueOf()
, like this:
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(number));
}
Because setText()
accepts only String values or Resource ID of a String
(which is infact int).
Check here: setText() Method
You can use String.valueOf(number);
as input parameter of setText()
or you can refer to String ID in XML with getResources().getString(R.string.number)
as input value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With