gpt4 book ai didi

java - 在 Android 应用程序中添加 Java 部分的 Intent 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:10 25 4
gpt4 key购买 nike

我是编码初学者。我正在学习如何在Android Studio上开发Android应用程序,这是java部分。我正在一步一步地遵循 udacity corse,但我不知道如何修复该错误。 请有人帮我修复它并解释我如何修复它?当我添加 Intent 部分时,出现了错误。 谢谢。

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]: /image/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>

最佳答案

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 条件没有右括号。

关于java - 在 Android 应用程序中添加 Java 部分的 Intent 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52543485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com