我是一个初学编码的人。我正在学习如何在Android Studio上开发Android应用程序,这是Java部分。我正在一步一步地跟踪一个Udacity Corse,但我不知道如何修复错误。
请有人能帮我修一下并告诉我怎么修吗?
添加意向部分时出现错误。
谢谢您。
package com.example.android.justjava;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
if (quantity == 100) {
// Show an error message as a toast
Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
// Exit this method early because there's nothing left to do
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}
/**
* This method is called when the minus button is clicked.
*/
public void decrement(View view) {
if (quantity == 1) {
// Show an error message as a toast
Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
// Exit this method early because there's nothing left to do
return;
}
quantity = quantity - 1;
displayQuantity(quantity);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
EditText text = (EditText) findViewById(R.id.Name_field);
String value = text.getText().toString();
// Figure out if the user wants whipped cream topping
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
// Figure out if the user wants chocolate topping
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.Chocolate_checkbox);
boolean hasChocolate = chocolateCheckBox.isChecked();
// Calculate the price
int price = calculatePrice( hasChocolate,hasWhippedCream);
// Display the order summary on the screen
String message = createOrderSummary (value, price, hasWhippedCream, hasChocolate);
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, "justJava order" + Name );
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
displayMessage(message);
}
/**
* Calculates the price of the order.
*
* @return total price
*/
private int calculatePrice(boolean hasChocolate, boolean hasWhippedCream) {
int basePrice = 5;
if (hasWhippedCream){
basePrice = basePrice +1;
}
if (hasChocolate){
basePrice = basePrice+2;
}
return quantity * basePrice;
}
/**
* Create summary of the order.
*
* @param price of the order
* @param addWhippedCream is whether or not to add whipped cream to the coffee
* @param addChocolate is whether or not to add chocolate to the coffee
* @return text summary
*/
private String createOrderSummary(String Name, int price, boolean addWhippedCream, boolean addChocolate) {
String priceMessage = "Name: " + Name;
priceMessage += "\nAdd whipped cream? " + addWhippedCream;
priceMessage += "\nAdd chocolate? " + addChocolate;
priceMessage += "\nQuantity: " + quantity;
priceMessage += "\nTotal: $" + price;
priceMessage += "\nThank you!";
return priceMessage;
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int numberOfCoffees) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + numberOfCoffees);
}
/**
* 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);
}
}[enter image description here][1]
[1]: https://i.stack.imgur.com/Uph4m.jpg
This is the XML part:
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"
>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:id="@+id/Name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textCapSentences"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="Toppings"
android:textAllCaps="true" />
android:id="@+id/whipped_cream_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="whipped cream"
android:paddingLeft="24dp"
android:textSize="16sp"
/>
android:id="@+id/Chocolate_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chocolate"
android:paddingLeft="24dp"
android:textSize="16sp"
/>
android:id="@+id/number"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Quantity"
android:textAllCaps="true" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"
android:orientation="horizontal"
android:text="-" />
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:textColor="@android:color/black"
android:orientation="horizontal"
android:textSize="16sp" />
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+" />
android:id="@+id/order_summary_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Order Summary"
android:textAllCaps="true" />
android:id="@+id/orderSummaryTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0$"
android:textColor="@android:color/black"
android:textSize="16sp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:text="Order" />