String.format输出的字符串toFloat引发的闪退

问题描述

应用设置中将多语言切换成阿拉伯语语言下,字符串转float出现闪退

闪退日志

java.lang.RuntimeException: Unable to pause activity {xxx.xxx.xxx/xxx.xxx.xxx.xxx.xxx.xxx}: java.lang.NumberFormatException: For input string: "674,0" at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:5255) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:5206) at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:5157) at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:46) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8633) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) Caused by: java.lang.NumberFormatException: For input string: "674,0" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122) at java.lang.Float.parseFloat(Float.java:451) at xxx.xxx.xxx.xxx.xxx.xxx.xxx(SourceFile:6) at xxx.xxx.xxx.xxx.xxx.xxx.xxx(SourceFile:10) at xxx.xxx.xxx.xxx.xxx.xxx.onPause(SourceFile:8) at androidx.fragment.app.Fragment.performPause(SourceFile:7) at androidx.fragment.app.FragmentStateManager.pause(SourceFile:3) at androidx.fragment.app.FragmentStateManager.moveToExpectedState(SourceFile:24) at androidx.fragment.app.FragmentStore.moveToExpectedState(SourceFile:3) at androidx.fragment.app.FragmentManager.moveToState(SourceFile:5) at androidx.fragment.app.FragmentManager.dispatchStateChange(SourceFile:3) at androidx.fragment.app.FragmentManager.dispatchPause(Unknown Source:1) at androidx.fragment.app.FragmentController.dispatchPause(SourceFile:1) at androidx.fragment.app.FragmentActivity.onPause(SourceFile:3) at xxx.xxx.xxx.xxx.onPause(SourceFile:1) at xxx.xxx.xxx.xxx.xxx.onPause(SourceFile:1) at android.app.Activity.performPause(Activity.java:8392) at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1510) at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:5245) ... 12 more java.lang.NumberFormatException: For input string: "674,0" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122) at java.lang.Float.parseFloat(Float.java:451) at xxx.xxx.xxx.xxx.xxx.xxx.xxx(SourceFile:6) at xxx.xxx.xxx.xxx.xxx.xxx.xxx(SourceFile:10) at xxx.xxx.xxx.xxx.xxx.xxx.onPause(SourceFile:8) at androidx.fragment.app.Fragment.performPause(SourceFile:7) at androidx.fragment.app.FragmentStateManager.pause(SourceFile:3) at androidx.fragment.app.FragmentStateManager.moveToExpectedState(SourceFile:24) at androidx.fragment.app.FragmentStore.moveToExpectedState(SourceFile:3) at androidx.fragment.app.FragmentManager.moveToState(SourceFile:5) at androidx.fragment.app.FragmentManager.dispatchStateChange(SourceFile:3) at androidx.fragment.app.FragmentManager.dispatchPause(Unknown Source:1) at androidx.fragment.app.FragmentController.dispatchPause(SourceFile:1) at androidx.fragment.app.FragmentActivity.onPause(SourceFile:3) at xxx.xxx.xxx.xxx.onPause(SourceFile:1) at xxx.xxx.xxx.xxx.xxx.onPause(SourceFile:1) at android.app.Activity.performPause(Activity.java:8392) at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1510) at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:5245) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:5206) at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:5157) at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:46) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8633) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

问题代码

val resultStart = String.format("%.1f", playPart.start.toFloat() / 1000).toFloat()
val resultEnd = String.format("%.1f", playPart.end.toFloat() / 1000).toFloat()

问题分析

  1. String.format调用的时候没有制定Locale
  2. format输出的字符串会已当前设置的语言转换
  3. 阿拉伯语言对数字数据会已,分隔,如674,0
  4. 如果674,0 直接调用toFloat(),就会由于解析不了,而抛出异常

解决方案

调用toFloat()方法前,去除','

fun String.toSafeFloat(): Float {
  var value = 0f
  try {
    value = replace(",", "").toFloat()
  } catch (e: Exception) {
    e.printStackTrace()
  }
  return value
}

你可能感兴趣的:(python,开发语言)