繁体   English   中英

java.lang.NoSuchMethodError:android中没有虚拟方法getTextBounds错误

[英]java.lang.NoSuchMethodError: No virtual method getTextBounds Error in android

当我尝试在 Android 中使用Paint类的getTextBounds()方法时,我在旧版本的模拟器(Marshmallow 和 Nougat 7.1.1)上遇到错误。 这是错误:

java.lang.NoSuchMethodError: No virtual method getTextBounds(Ljava/lang/CharSequence;IILandroid/graphics/Rect;)V in class Landroid/graphics/Paint; or its super classes (declaration of 'android.graphics.Paint' appears in /system/framework/framework.jar)

这个问题似乎没有发生在 android 10 模拟器上。 我尝试按照另一个答案中的建议使缓存无效并重新启动,但它没有用。

此方法是在 Android api 28 中引入的 - 请在此处查看- 这意味着它在之前的版本中不可用。

这将适用于运行 api 28+ 的设备,并将在运行较低 api 级别的设备中抛出该异常。

通常正确的做法是引入版本检查:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// Safe to use getMccString
} else {
// Use something else that could work if there's something
}

请注意,仅仅因为您可以在您的机器中浏览源代码,并不意味着运行您的应用程序的设备将运行相同的 Android 代码 - 大多数情况下不会。

正如这里提到的

当我使用

Paint.getTextBounds(@NoNull CharSequence text, int start, int end, @NoNull Rect bounds); 

一些设备抛出

error:java.lang.NoSuchMethodError: No virtual method getTextBounds

代替:

Paint.getTextBounds(@NoNull String text, int start, int end, @NoNull Rect bounds);

它有效希望帮助你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM