首页 > 解决方案 > 在 Android 应用程序中添加关于 Java 部分的意图时出错

问题描述

我是编码的初学者。我正在学习如何在 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: 

<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"
  >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/Name_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name"
        android:inputType="textCapSentences"/>

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:text="Toppings"
        android:textAllCaps="true" />

    <CheckBox
        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"
       />

    <CheckBox
        android:id="@+id/Chocolate_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chocolate"
        android:paddingLeft="24dp"
        android:textSize="16sp"
        />

    <TextView
        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" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:onClick="decrement"
            android:orientation="horizontal"
            android:text="-" />

    <TextView
        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" />

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:onClick="increment"
            android:text="+" />


    </LinearLayout>


    <TextView
        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" />

    <TextView
        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" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:onClick="submitOrder"
        android:text="Order" />

</LinearLayout>
</ScrollView>

标签: javaandroidandroid-studioandroid-intentservice

解决方案


intent.putExtra(Intent.EXTRA_SUBJECT, "justJava order" + Name ); // NAME is not defined
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    displayMessage(message);
   }// THIS IS MISSING   
 }

1)你还没有声明Name变量。

2)您的 if 条件没有右括号。


推荐阅读