【发布时间】:2017-01-21 00:54:27
【问题描述】:
您好,当我选中该复选框时,应用程序崩溃了,但程序正常运行
XML:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/whippedCreamTopping"
android:onClick="onCheckboxClicked"
/>
Java:
//This method is called when the order button is clicked.
public void submitOrder(View view) {
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whippedCreamTopping);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
int price = calculatePrice();
String priceMessage = createOrderSummary(price, hasWhippedCream);
displayMessage(priceMessage);
}
//displays the given quantity value on the screen.
private void displayQuantity(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
//Calculates the price of the order.
private int calculatePrice() {
return quantity * price;
}
//Add in a summary
private String createOrderSummary(int price, boolean addWCream) {
double priceTotal = calculatePrice();
String myName = "JJ Jobs";
String priceString = "Name: " + myName;
priceString += "\nAdd Whipped Cream?" + addWCream;
priceString += "\nQuantity: " + quantity + "\nTotal: $" + priceTotal;
priceString += "\nThank you";
return priceString;
} //end of createOrderSummary
我试过How to check if android checkbox is checked within its onClick method (declared in XML)? 但我没有得到代码(复选框起作用)
【问题讨论】:
-
发布 logcat 错误日志以及 onCheckboxClicked 方法
-
你有视图那么你为什么要为复选框创建另一个新实例
-
你需要发布你得到的错误和错误的行号。其次,您根据 xml 的复选框方法是“onCheckboxClicked”,但我在您的 java 类中找不到它