2017-03-09 10 views
0

アンドロイドスタジオでコーヒーを注文するアプリをやっていて、文字列をstrings.xmlに変更してハードコードされていない限り、完璧になりました。今では私の携帯電話で起動し、それを試す前にアンドロイドスタジオにエラーが表示されませんが、電話ボタンのsubmitOrderを押すたびにクラッシュします。それは私を与えるエラーは次のとおりです。文字列をstrings.xmlに変換するとエラーが発生する

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は次のとおりです。

<?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は次のとおりです:

/** 
* <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"?> 
<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

UnknownFormatConversionExceptionが、(あなたのXMLファイルで)指定された形式にはgetString()に渡された自分の引数変数を、変換することができなかったことを意味strings.xmlファイルから文字列値を取得します。

整数の書式設定にはdを使用する必要があります。

あなたがgetResourcesを(追加する必要が
+0

これは完璧です、ありがとうございました –

0

追加する必要がありますgetResources()以下のように、以下の

getResources().getString(R.string.order_summary_whipped_cream, addWhippedCream); 
0

)、getStringメソッドの前に、それは

0
MainActivity.this.getResources().getString(R.string.order_summary_whipped_cream, addWhippedCream); 

は、常に現在の活動のためのgetResourcesを使用してリソースを修飾していきます。

+0

フォーマットエラー例外があり、無効なデータが使用されています。 – Remario

+0

%iと%dも同じですが、%iは主に小数点以下の桁数に使用されます。 %dを試してください。あなたの価格はintです – Remario

+0

あなたの数量も整数です、orderSummary = orderSummary + "\ n" + getString(R.string.order_summary_quantity、quantity); strings.xmlファイルで%iの代わりに%dを使用してください。 – Remario

0

strings.xmlで宣言された合計文字列で%iを%1 $ dに置き換えなければならないと思います。 フォーマットエラーがあり、実際にフォーマットパラメータが取得されていません。 参考:Android:StringFormatting

0

意味は%iとなりますか? numberには%dである必要があります。

+0

ええ、それは、ありがとう –

関連する問題