java.lang.NoSuchMethodError:android中没有虚拟方法getTextBounds错误
当我尝试在Android中使用java.lang.NoSuchMethodError:android中没有虚拟方法getTextBounds错误,java,android,textview,android-paint,Java,Android,Textview,Android Paint,当我尝试在Android中使用Paint类的getTextBounds()方法时,我在旧版本的模拟器(棉花糖和牛轧糖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 (dec
Paint
类的getTextBounds()
方法时,我在旧版本的模拟器(棉花糖和牛轧糖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 emulator上。我试着按照另一个答案中的建议使缓存失效并重新启动,但没有成功。这个方法是在Android api 28-check中引入的,这意味着它在以前的版本中不可用 这将适用于运行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);
它起作用了
希望对你有所帮助那么,28岁之前有没有推荐的方法?我只需要textStudy Android源代码的宽度,Paint.java源代码,请参阅measureText和getTextBounds方法。你会知道,测量文本调用NATEVER测量文本,GETTrimeBuffic调用NATESTORGSTRIGIN界限,这是C++实现的本机方法。这是正确答案。