我使用android studio.i开发了我的应用程序没有找到任何语法错误但应用程序无法正常工作请帮助我。每当我按下应用程序中的任何按钮时它显示“遗憾的是应用程序停止了”
public class MainActivity extends ActionBarActivity {
int quantity=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitOrder(){
EditText nameField = (EditText)findViewById(R.id.Enter_your_name);
String name = nameField.getText().toString();
CheckBox whippedCreamCheckBox =(CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream=whippedCreamCheckBox.isChecked();
CheckBox chocolateCheckBox =(CheckBox) findViewById(R.id.Chocolate_checkbox);
boolean hasChocolate=chocolateCheckBox.isChecked();
int price=calculatePrice(quantity,hasWhippedCream,hasChocolate);
String priceMessage=createOrderSummary(name,price,hasWhippedCream,hasChocolate);
displayMessage(priceMessage);
}
public void increment(View view){
quantity=quantity+1;
displayQuantity(quantity);
}
public void decrement(View view){
quantity+=+1;
displayQuantity(quantity);
}
private void displayMessage(String message){
TextView orderSummaryTextView=(TextView) findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText(message);}
private void displayQuantity(int numberOfCoffees){
TextView quantityTextView= (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(numberOfCoffees);
}
private String createOrderSummary(String name,int price,boolean addWhippedCream,boolean hasChocolate) {
String priceMessage="Name:"+name;
priceMessage+="\nAdd whipped cream?" + addWhippedCream;
priceMessage+="\nAdd Chocolate?" + hasChocolate;
priceMessage+="\nquantity:"+quantity;
priceMessage+="\nTotal:"+price;
priceMessage+="\n Thank you!";
return priceMessage;
}
private int calculatePrice(int quantity,boolean addWhippedCream,boolean addChocolate){
int basePrice=5;
if(addWhippedCream){
basePrice=basePrice+1;
}
if (addChocolate){
basePrice=basePrice+2;
}
return basePrice*quantity;
}
}
XML file :
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<EditText
android:id="@+id/Enter_your_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="name"
android:inputType="textCapWords"/>
<CheckBox
android:id="@+id/whipped_cream_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Whipped cream"
android:layout_marginTop="16dp"/>
<CheckBox
android:id="@+id/Chocolate_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chocolate"
android:layout_marginTop="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="Quantity"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"
android:text="-" />
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="0"
android:textColor="@android:color/black"
android:textSize="16sp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="order summary"
android:textSize="16sp"
android:textAllCaps="true" />
<TextView
android:id="@+id/order_summary_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="$0"
android:textColor="@android:color/black"
android:textSize="16sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:text="Order" />
</LinearLayout>
</ScrollView>
答案 0 :(得分:1)
submitOrder()应该有一个参数(View view)