Android本地化字符串格式错误求助:订单摘要方法异常排查
Hey there! Let's break down the issues in your code that are causing the localization format error, and walk through how to fix them step by step.
Key Problems in Your Current Code
- Hardcoded non-localized text: You're only using
getString()for the name field, but the rest of the order summary text (like "add whipped cream?") is hardcoded as plain strings. This not only breaks localization for other languages but also increases the chance of formatting inconsistencies. - Incomplete variable name: Your code cuts off at
"\nQuantity : " + quanti...— it looks like you meant to use aquantityvariable here, which is a spelling/typo error that will cause runtime issues. - Inefficient and risky string concatenation: Using
+=to build your string can lead to performance issues (especially if this runs frequently) and makes it harder to manage localized formatting (like line breaks that might vary by language).
Step-by-Step Fix
1. Update Your strings.xml with Localizable Templates
First, move all order summary text into your res/values/strings.xml (and corresponding locale-specific files) with proper placeholders:
<!-- res/values/strings.xml --> <string name="order_summary_name">Name: %1$s</string> <string name="order_summary_whipped_cream">Add whipped cream? %2$b</string> <string name="order_summary_chocolate">Add chocolate? %3$b</string> <string name="order_summary_quantity">Quantity: %4$d</string> <string name="order_summary_total">Total Price: $%5$d</string> <!-- Optional: A single full template for cleaner code --> <string name="order_summary_full">%1$s\n%2$s\n%3$s\n%4$s\n%5$s\nThank you!</string>
Note: The numbered placeholders (%1$s, %2$b) ensure variables map correctly even if the string order changes for different locales.
2. Fix Your Java Code
Option 1: Build the summary using individual localized strings (good for flexibility):
private String createOrderSummary(String name, int price, boolean addWhippedCream, boolean addChocolate, int quantity) { // Use StringBuilder for efficient string building StringBuilder priceMessage = new StringBuilder(); priceMessage.append(getString(R.string.order_summary_name, name)) .append("\n") .append(getString(R.string.order_summary_whipped_cream, addWhippedCream)) .append("\n") .append(getString(R.string.order_summary_chocolate, addChocolate)) .append("\n") .append(getString(R.string.order_summary_quantity, quantity)) .append("\n") .append(getString(R.string.order_summary_total, price)); return priceMessage.toString(); }
Option 2: Use a single full template for cleaner code:
private String createOrderSummary(String name, int price, boolean addWhippedCream, boolean addChocolate, int quantity) { // Pull the full localized template and fill all placeholders at once return getString(R.string.order_summary_full, getString(R.string.order_summary_name, name), getString(R.string.order_summary_whipped_cream, addWhippedCream), getString(R.string.order_summary_chocolate, addChocolate), getString(R.string.order_summary_quantity, quantity), getString(R.string.order_summary_total, price)); }
3. Verify Placeholder Type Matching
Double-check that each placeholder in strings.xml matches the variable type you're passing:
%sfor String values (likename)%dfor integer values (likeprice,quantity)%bfor boolean values (likeaddWhippedCream)
Mismatched types will cause runtime exceptions or incorrect display text.
内容的提问来源于stack exchange,提问作者user9666156

