Android:西班牙语:解析浮点值时出现问题:应用程序崩溃
Android: Spanish: issue while parsing float value : App crashes
Android:西班牙语:解析浮点值时出现问题:应用程序崩溃
脚步:
1.set 应用程序语言为西班牙语
2.format 一些浮点值到一位小数
3.parse 格式化值再次浮动
应用崩溃。
示例代码如下:
如果您对此有任何想法,请提供帮助。
TextView textView = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
float value = 3.456789f;
setLocale("es", this);//Set App language as Spanish
String parsedString = String.format("%.1f", value); // format the float value to single precision
float parsedValueFloat = Float.parseFloat(parsedString); // parse the value again to float.(APP Crashesh here)
textView.setText(parsedValueFloat+"");
}
public static void setLocale(String languageCode, Context ctx) {
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
ctx.getResources().updateConfiguration(config,
ctx.getResources().getDisplayMetrics());
}
崩溃日志如下:
01-06 20:41:00.265: E/AndroidRuntime(3153): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testcanvas/com.example.testcanvas.MainActivity}: java.lang.NumberFormatException: Invalid float: "3,5"
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.access0(ActivityThread.java:159)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.os.Looper.loop(Looper.java:137)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.reflect.Method.invoke(Method.java:525)
01-06 20:41:00.265: E/AndroidRuntime(3153): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-06 20:41:00.265: E/AndroidRuntime(3153): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-06 20:41:00.265: E/AndroidRuntime(3153): at dalvik.system.NativeStart.main(Native Method)
01-06 20:41:00.265: E/AndroidRuntime(3153): Caused by: java.lang.NumberFormatException: Invalid float: "3,5"
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.StringToReal.parseFloat(StringToReal.java:310)
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.Float.parseFloat(Float.java:300)
01-06 20:41:00.265: E/AndroidRuntime(3153): at com.example.testcanvas.MainActivity.onCreate(MainActivity.java:31)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.Activity.performCreate(Activity.java:5372)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
01-06 20:41:00.265: E/AndroidRuntime(3153): ... 11 more
发生这种情况是因为 floats/doubles 到字符串的本地化输出,在这种情况下(以及许多其他语言 - 您已经注意到西班牙语、意大利语、俄语,还有法语和其他语言),使用逗号而不是比句点:
3,5
而不是
3.5
由于 Float.parse()
需要一个 "en" 区域设置格式的字符串,执行此操作的方法是使用 NumberFormat
class, as per this answer:
NumberFormat nf = NumberFormat.getInstance(Locale.forLanguageTag("es"));
Number parsedNumber = nf.parse(parsedString);
float parsedValueFloat = parsedNumber.floatValue();
Android:西班牙语:解析浮点值时出现问题:应用程序崩溃 脚步:
1.set 应用程序语言为西班牙语
2.format 一些浮点值到一位小数
3.parse 格式化值再次浮动
应用崩溃。
示例代码如下:
如果您对此有任何想法,请提供帮助。
TextView textView = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
float value = 3.456789f;
setLocale("es", this);//Set App language as Spanish
String parsedString = String.format("%.1f", value); // format the float value to single precision
float parsedValueFloat = Float.parseFloat(parsedString); // parse the value again to float.(APP Crashesh here)
textView.setText(parsedValueFloat+"");
}
public static void setLocale(String languageCode, Context ctx) {
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
ctx.getResources().updateConfiguration(config,
ctx.getResources().getDisplayMetrics());
}
崩溃日志如下:
01-06 20:41:00.265: E/AndroidRuntime(3153): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testcanvas/com.example.testcanvas.MainActivity}: java.lang.NumberFormatException: Invalid float: "3,5"
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.access0(ActivityThread.java:159)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.os.Looper.loop(Looper.java:137)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.reflect.Method.invoke(Method.java:525)
01-06 20:41:00.265: E/AndroidRuntime(3153): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-06 20:41:00.265: E/AndroidRuntime(3153): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-06 20:41:00.265: E/AndroidRuntime(3153): at dalvik.system.NativeStart.main(Native Method)
01-06 20:41:00.265: E/AndroidRuntime(3153): Caused by: java.lang.NumberFormatException: Invalid float: "3,5"
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.StringToReal.parseFloat(StringToReal.java:310)
01-06 20:41:00.265: E/AndroidRuntime(3153): at java.lang.Float.parseFloat(Float.java:300)
01-06 20:41:00.265: E/AndroidRuntime(3153): at com.example.testcanvas.MainActivity.onCreate(MainActivity.java:31)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.Activity.performCreate(Activity.java:5372)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-06 20:41:00.265: E/AndroidRuntime(3153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
01-06 20:41:00.265: E/AndroidRuntime(3153): ... 11 more
发生这种情况是因为 floats/doubles 到字符串的本地化输出,在这种情况下(以及许多其他语言 - 您已经注意到西班牙语、意大利语、俄语,还有法语和其他语言),使用逗号而不是比句点:
3,5
而不是
3.5
由于 Float.parse()
需要一个 "en" 区域设置格式的字符串,执行此操作的方法是使用 NumberFormat
class, as per this answer:
NumberFormat nf = NumberFormat.getInstance(Locale.forLanguageTag("es"));
Number parsedNumber = nf.parse(parsedString);
float parsedValueFloat = parsedNumber.floatValue();