I\'m learning Android Studio (Java and XML) and am attempting to create a simple
ID: 3908622 • Letter: I
Question
I'm learning Android Studio (Java and XML) and am attempting to create a simple drink ordering app where each drink costs 2 dollars and adding a topping costs 25 cents each. Once the order button is pressed, the price is supposed to update. However, I get the error: Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.CheckBox. How do I determine whether the boxes were checked and display the price when the order button is clicked?
Java Section:
Just Java QUANTITY Whipper Cm Curamel Sauc Covolae Sauce PRICE ORDERExplanation / Answer
You are getting this error because you are trying to access the Checkbox view on Button click. The button view does not have any isChecked() method, hence it throws the exception.
Hence, you would need to declare a global variable for price and add 25 to it whenever a checkbox is selected. Once the user presses the ORDER button, just display the price (instead of calculating the price when the button is clicked).
A sample run is given below:
MainActivity.java
***********************
activity_main.xml
*********************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.