Android studio的应用程序崩溃时 CheckBox 检查

发布时间: 2016/9/28 2:22:51
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

嗨应用程序崩溃时我检查 checkbox ,但它否则为程序工作

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

我试过如何检查如果 android checkbox 检查其 onClick 方法内 (在中声明 XML )?但得不到的代码 ( checkbox 上班)

解决方法 1:

我发现里面问题 XML 的代码︰

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/whippedCreamTopping"

        android:onClick="onCheckboxClicked"  *I removed this line of code 
                                             *then it worked
        />  

谢谢你的帮助

官方微信
官方QQ群
31647020