我正在做一个在Android工作室订购咖啡的应用程序,它一直都很完美,直到我将字符串更改为strings.xml,因此它们没有硬编码。现在它开始在我的手机上,并且在尝试之前android studio中没有出现任何错误,但每当我按下电话按钮中的submitOrder时它就会崩溃。它给我的错误是:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.justjava, PID: 12720
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4508)
at android.view.View$PerformClick.run(View.java:18675)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4508)
at android.view.View$PerformClick.run(View.java:18675)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.util.UnknownFormatConversionException: Conversion: i
at java.util.Formatter$FormatToken.unknownFormatConversionException(Formatter.java:1399)
at java.util.Formatter$FormatToken.checkFlags(Formatter.java:1336)
at java.util.Formatter.transform(Formatter.java:1442)
at java.util.Formatter.doFormat(Formatter.java:1081)
at java.util.Formatter.format(Formatter.java:1042)
at java.util.Formatter.format(Formatter.java:1011)
at java.lang.String.format(String.java:1999)
at android.content.res.Resources.getString(Resources.java:1527)
at android.content.Context.getString(Context.java:370)
at com.example.android.justjava.MainActivity.createOrderSummary(MainActivity.java:138)
at com.example.android.justjava.MainActivity.submitOrder(MainActivity.java:67)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4508)
at android.view.View$PerformClick.run(View.java:18675)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Mainactivity.java是:
/**
* <p>
* package com.example.android.justjava;
*/
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.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.NumberFormat;
import static android.R.attr.name;
import static android.R.id.message;
import static android.content.Intent.ACTION_SENDTO;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 1;
int priceOfOneCup = 5;
int whippedCreamPrice = 1;
int chocolatePrice = 2;
;
@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) {
//get the name the user has written
EditText nameEditText = (EditText) findViewById(R.id.name_field);
String name = nameEditText.getText().toString();
//figure out if the user wants whipped cream
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.checkbox_whippedCream);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
//figure out if the user wants chocolate
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.checkbox_chocolate);
boolean hasChocolate = chocolateCheckBox.isChecked();
int price = calculatePrice(hasWhippedCream, hasChocolate);
Intent intent = new Intent(ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject, name));
intent.putExtra(Intent.EXTRA_TEXT, createOrderSummary(price, hasWhippedCream, hasChocolate, name));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
//You cannot order more than 100
if (quantity == 100){
//Show the client the error
Toast moreThan100 = Toast.makeText(this, getString(R.string.toast_too_much), Toast.LENGTH_SHORT );
moreThan100.show();
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}
/**
* This method is called when the minus button is clicked.
*/
public void decrement(View view) {
//You cannot order less than 1
if (quantity == 1){
//Show the client the error
Toast lessThan1 = Toast.makeText(getApplicationContext(), getString(R.string.toast_too_less), Toast.LENGTH_SHORT );
lessThan1.show();
return;
}
quantity = quantity - 1;
displayQuantity(quantity);
}
/**
* Calculates the price of the order.
*
* @return total price
*/
private int calculatePrice(boolean hasWhippedCream, boolean hasChocolate) {
int basePrice = priceOfOneCup;
if (hasWhippedCream) {
basePrice = basePrice + whippedCreamPrice;
}
if (hasChocolate) {
basePrice = basePrice + chocolatePrice;
}
int price = basePrice * quantity;
return price;
}
/**
* Create summary of the order.
*
* @param addWhippedCream is whether or not the user wants whipped cream topping
* @param addChocolate is whether or not the user wants whipped cream topping
* @param price of the order
* @return text summary
*/
private String createOrderSummary(int price, boolean addWhippedCream, boolean addChocolate, String name) {
String orderSummary = getString(R.string.order_summary_name, name);
orderSummary = orderSummary + "\n" + getString(R.string.order_summary_whipped_cream, addWhippedCream);
orderSummary = orderSummary + "\n" + getString(R.string.order_summary_chocolate, addChocolate);
orderSummary = orderSummary + "\n" + getString(R.string.order_summary_quantity, quantity);
orderSummary = orderSummary + "\n" + getString(R.string.order_summary_price, price);
orderSummary = orderSummary + "\n" + getString(R.string.thank_you);
return orderSummary;
}
/**
* 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 + "");
}
}
activity_main.xml是:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.justjava.MainActivity">
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:scrollbars="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name"
android:id="@+id/name_field"
android:inputType="textCapWords"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="@string/toppings"
android:textAllCaps="true" />
<CheckBox
android:id="@+id/checkbox_whippedCream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="@string/whippedCream"
android:textColor="@android:color/black"
android:textSize="16sp" />
<CheckBox
android:id="@+id/checkbox_chocolate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="@string/chocolate"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="@string/quantity"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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="@string/initial_quantity_value"
android:textColor="@android:color/black"
android:textSize="16sp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:text="@string/order" />
</LinearLayout>
</ScrollView>
最后,strings.xml是:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="http://schemas.android.com/apk/res-auto">
<string name="app_name">Just Java</string>
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="name">Name</string>
<!-- Heading for the toppings checkboxes [CHAR LIMIT=20] -->
<string name="toppings">Toppings</string>
<!-- Name of the CheckBox for whipped cream [CHAR LIMIT=20] -->
<string name="whippedCream">Whipped Cream</string>
<!-- Name of the CheckBox for chocolate [CHAR LIMIT=20] -->
<string name="chocolate">Chocolate</string>
<!-- Heading for the quantity controller [CHAR LIMIT=20] -->
<string name="quantity">Quantity</string>
<!-- Number of cups displayed in the beginning [CHAR LIMIT=5] -->
<string name="initial_quantity_value">1</string>
<!-- Heading for the ordering button [CHAR LIMIT=20] -->
<string name="order">Order</string>
<!-- Subject line for the order summary email. It will be in the format of
"Just Java order for Amy" where Amy is the user's name. [CHAR LIMIT=NONE] -->
<string name="email_subject">Just Java to <xliff:g id="name" example="Miguel Verdaguer">%s</xliff:g></string>
<!-- Toast message showing an error when more than 100 cups are ordered [CHAR LIMIT=40] -->
<string name="toast_too_much">You cannot order more than 100 cups</string>
<!-- Toast message showing an error when less than 1 cup is ordered [CHAR LIMIT=40] -->
<string name="toast_too_less">You cannot order less than 1 cup</string>
<!-- Name for the order summary. It will be shown in the format of "Name: Amy" where Amy is the
user's name. [CHAR LIMIT=NONE] -->
<string name="order_summary_name">Name: <xliff:g id="name" example="Miguel Verdaguer">%s</xliff:g></string>
<!-- Whipped cream topping for the order summary. It will be shown in the format of
"Add whipped cream? true" or "Add whipped cream? false". [CHAR LIMIT=NONE] -->
<string name="order_summary_whipped_cream">Add whipped cream? <xliff:g id="addWhippedCream" example="false">%b</xliff:g></string>
<!-- Chocolate topping for the order summary. It will be shown in the format of
"Add chocolate? true" or "Add chocolate? false". [CHAR LIMIT=NONE] -->
<string name="order_summary_chocolate">Add chocolate? <xliff:g id="addChocolate" example="false">%b</xliff:g></string>
<!-- Quantity for the order summary.It will be shown in the format of Quantity: 1. [CHAR LIMIT=NONE] -->
<string name="order_summary_quantity">Quantity: <xliff:g id="quantity" example="1">%i</xliff:g></string>
<!-- Price for the order summary.It will be shown in the format of Total: 10$. [CHAR LIMIT=NONE] -->
<string name="order_summary_price">Total: <xliff:g id="price" example="10">%i $</xliff:g></string>
<!-- Thank you message for the order summary. [CHAR LIMIT=NONE] -->
<string name="thank_you">Thank You!</string>
</resources>
答案 0 :(得分:0)
您需要添加getResources()
,如下所示,以获取strings.xml
文件
getResources().getString(R.string.order_summary_whipped_cream, addWhippedCream);
答案 1 :(得分:0)
UnknownFormatConversionException
表示它无法将传递给getString()的参数变量转换为指定的格式(在XML文件中)。
您应该使用d
格式化整数。
答案 2 :(得分:0)
你需要在getString方法之前添加getResources(),然后才能工作
答案 3 :(得分:0)
MainActivity.this.getResources().getString(R.string.order_summary_whipped_cream, addWhippedCream);
始终使用getResources为当前活动限定资源。
答案 4 :(得分:0)
我认为你必须在strings.xml中声明的Total字符串中用%1 $ d替换%i 有格式错误,它并没有真正获得格式参数。 供参考:Android:StringFormatting
答案 5 :(得分:0)
那里有什么意思%i
?数字应为%d
。