应用设置中将多语言切换成阿拉伯语语言下,字符串转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()
,
分隔,如674,0,
而抛出异常调用toFloat()方法前,去除','
fun String.toSafeFloat(): Float {
var value = 0f
try {
value = replace(",", "").toFloat()
} catch (e: Exception) {
e.printStackTrace()
}
return value
}