我的代碼不會編譯,我不知道如何解決它。變量addWhippedCream和addChocolate給我一個錯誤,當我嘗試將它們傳入calculatePrice方法從orderSummary方法。我試圖讓它們成爲全局變量,但這會產生運行時錯誤Android工作室canot解析符號,不會編譯
package com.example.android.justjava;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
int price=5;
EditText Name = (EditText)findViewById(R.id.name_view);
int total=calculatePrice(price, addWhippedCream, addChocolate);
CheckBox checkBox = (CheckBox) findViewById(R.id.whipped_cream_checkBox);
CheckBox checkBox2 = (CheckBox) findViewById(R.id.Chocolate_checkBox);
boolean addWhippedCream=checkBox.isChecked();
boolean addChocolate=checkBox2.isChecked();
String customerName= Name.getText().toString();
displayMessage (createOrderSummary(total,addWhippedCream,addChocolate,customerName));
}
int quantity = 2;
public void increment(View view){
quantity = quantity + 1;
if (quantity==100){
return;
}
displayQuantity(quantity);
}
public void decrement (View view){
quantity = quantity - 1;
if (quantity==1) {
return;
}
displayQuantity(quantity);
}
/**
* This method 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);
}
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText(message);
orderSummaryTextView.setTextColor(Color.WHITE);
}
private int calculatePrice(int price, boolean addWhippedCream, boolean addChocolate){
int total = quantity * price;
if (addWhippedCream){
total=total+1;
}
if (addChocolate) {
total=total+2;
}
return total;
}
/*creates orderSummary
*
* takes in int total, boolean isChecked, boolean isChecked2, String customerName
*
* @returns orderSummary
*/
private String createOrderSummary(int total, boolean addWhippedCream, boolean addChocolate, String customerName){
String orderSummary = "Name: " + customerName +
"\nAdd whipped cream? " + addWhippedCream +
"\nAdd chocolate? " + addChocolate +
"\nQuantity: " + quantity +
"\nTotal: $" + total +
"\nThank you!";
return orderSummary;
}
}
之前。在聲明兩個布爾變量之後調用該方法。 – DsD