java.lang.NoSuchMethodError: 在安卓中出现了没有虚拟方法 getTextBounds 的错误。

huangapple go评论73阅读模式
英文:

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模拟器上不会出现。我尝试了无效化缓存并重新启动,正如另一个答案中建议的,但并没有起作用。

英文:

When I try to use the getTextBounds() method of Paint class in Android, I am getting errors on older version emulators (Marshmallow and Nougat 7.1.1). This is the error:

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)

The issue does not seem to happen on android 10 emulator. I tried Invalidating caches and restarting as suggested in another answer, but it did not work.

答案1

得分: 2

以下是翻译好的部分:

当我使用以下代码时:

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);

就可以正常工作了。希望能帮到你。

英文:

When I use

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

some devices throws

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

replace:

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

it works
hope help U

答案2

得分: 1

这种方法是在Android API 28中引入的 - 在这里查看1 - 这意味着在之前的版本中不可用。

这将适用于运行API 28+的设备,并且在运行较低API级别的设备上会抛出异常。

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    // 可以安全使用getMccString
} else {
    // 使用其他可能适用的方法
}

请注意,仅因为您可以在自己的计算机上浏览源代码,并不意味着运行您的应用的设备上将具有相同的Android代码 - 大多数情况下不会。

正如此处所提到的2

英文:

This method was introduced in Android api 28 - check here - which means it won't be available in versions before.

This will work on devices running api 28+ and will throw that exception in devices running lower api levels.

Usually the correct way to do this is to introduce checks for the version:

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

Note that just because you can browse the source in your machine it doesn't mean the device running your app will have the same Android code running - most of the time it doesn't.

as mention here

huangapple
  • 本文由 发表于 2020年10月22日 16:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64478692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定