Android Math.toIntExact 实用方法的版本兼容性
Android version compatibility for Math.toIntExact Utility method
Math.toIntExact
方法在我的 Android 6.0 模拟器上抛出异常。 (较新的模拟器版本可以)
This Microsoft doc 是我找到的唯一关于它的文档。我是否认为 Math.toIntExact
在 Android 9.0 之前不兼容?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dmurphy.remotescrumpoker, PID: 9527
java.lang.NoSuchMethodError: No static method toIntExact(J)I in class Ljava/lang/Math; or its super classes (declaration of 'java.lang.Math' appears in /system/framework/core-libart.jar)
at com.dmurphy.remotescrumpoker.Activity_TeamDetails.onComplete(Activity_TeamDetails.java:268)
at com.google.android.gms.tasks.zzj.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D/FA: Event not sent since app measurement is disabled
根据文档,Math::toIntExact
已在 API 级别 24 中引入,即 Android 7.0。是的,它在 Android 6.
上不可用
好消息是您可以通过从 java.lang.Math
source code.
中提取它来轻松地自己实现它
public static int toIntExact(long value) {
if ((int)value != value) {
throw new ArithmeticException("integer overflow");
}
return (int)value;
}
Math.toIntExact
方法在我的 Android 6.0 模拟器上抛出异常。 (较新的模拟器版本可以)
This Microsoft doc 是我找到的唯一关于它的文档。我是否认为 Math.toIntExact
在 Android 9.0 之前不兼容?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dmurphy.remotescrumpoker, PID: 9527
java.lang.NoSuchMethodError: No static method toIntExact(J)I in class Ljava/lang/Math; or its super classes (declaration of 'java.lang.Math' appears in /system/framework/core-libart.jar)
at com.dmurphy.remotescrumpoker.Activity_TeamDetails.onComplete(Activity_TeamDetails.java:268)
at com.google.android.gms.tasks.zzj.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D/FA: Event not sent since app measurement is disabled
Math::toIntExact
已在 API 级别 24 中引入,即 Android 7.0。是的,它在 Android 6.
好消息是您可以通过从 java.lang.Math
source code.
public static int toIntExact(long value) {
if ((int)value != value) {
throw new ArithmeticException("integer overflow");
}
return (int)value;
}