java “调用需要 API 级别 23”错误,但 API 1 的 FrameLayout 上存在 getForeground()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33379534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:35:17  来源:igfitidea点击:

'Call requires API level 23' error, but getForeground() exists on FrameLayout from API 1

javaandroidandroid-studioandroid-framelayoutandroid-api-levels

提问by albodelu

Summary

概括

Finally, I found the related issue, setForeground()incorrectly flagged as requiring API 23 on ViewGroups extending FrameLayout.

最后,我发现了相关问题,setForeground()错误地标记为在 ViewGroups 扩展上需要 API 23 FrameLayout

Issue 189041: setForeground() incorrectly flagged as requiring API 23 (NewApi) for ViewGroups extending FrameLayout

问题 189041:setForeground() 错误地标记为需要 API 23 (NewApi) 以用于扩展 FrameLayout 的 ViewGroups

If you are not extending FrameLayout, the documentation is wrong and API 23 is required.

如果您没有扩展FrameLayout,则文档是错误的并且需要 API 23。

Issue 186273: View.setForeground wrong API level

问题 186273:View.setForeground 错误的 API 级别

Updated question

更新的问题

Marked as duplicated of Can't use setForegroundmethod on ImageViewby Commonsware and answered by him:

被Commonsware标记为无法setForeground在ImageView上使用方法的重复并由他回答:

That is a documentation bug. setForeground() existed on FrameLayout from API Level 1; it is only on View as of API Level 23.

这是一个文档错误。setForeground() 存在于 API 级别 1 的 FrameLayout 上;它仅适用于 API 级别 23 的视图。

In my case the documentation bug is not detected by the inspection in the fork but does it in an integration project, I don't understand it, anyway, this answers my initial question.

在我的情况下,fork 中的检查未检测到文档错误,但在集成项目中检测到了文档错误,我不明白,无论如何,这回答了我最初的问题。

But It's not the same case, this code is already extending a Framelayoutand using this method, it's not an ImageView, so I suppose the method has been removed in API 23. It doesn't appear in the reference now.

但情况不一样,这段代码已经扩展了 aFramelayout并且使用了这个方法,它不是 an ImageView,所以我想这个方法已经在 API 23 中删除了。它现在没有出现在参考中。

Update:I add a diff reportfor FrameLayout:

更新:我为 FrameLayout添加了一个差异报告

enter image description here

在此处输入图片说明

New question

新问题

So my question changes to how to use the method in previous versions, and, why does appear the inspection error selectively?

所以我的问题变成了如何使用以前版本中的方法,以及,为什么有选择地出现检查错误?

If I add a redundant cast, the error message disappears:

如果我添加一个多余的演员,错误信息就会消失:

((FrameLayout) layout).getForeground()

But it's accessing the Viewmethod that is not supported now.

但它正在访问View现在不支持的方法。

So I suppose targeting a previous API would remove the error message but I would appreciate a solution to use this Framelayout method or a clue to fix the issue in my case:

因此,我想针对以前的 API 会删除错误消息,但我会很感激使用此 Framelayout 方法的解决方案或解决我的问题的线索:

ANDROID_COMPILE_SDK_VERSION=23
ANDROID_BUILD_TOOLS_VERSION=23.0.1
ANDROID_DEFAULT_MIN_SDK_VERSION=19
ANDROID_DEFAULT_TARGET_SDK_VERSION=23

Workaround

解决方法

In short, as a workaround, I changed the type to FrameLayoutdirectly, so warnings about API and casting are gone, and I tested it works on Android 4.4.4 device and debugging, I thought it would crash, anyway it's only used in one animation, and I will fix/replace it in the future.

总之,作为一种解决方法,我将类型更改为FrameLayout直接,因此有关API和强制转换的警告消失了,我在Android 4.4.4设备和调试上测试了它的工作原理,我认为它会崩溃,反正它只在一个动画中使用,我会在未来修复/替换它。

Initial question

初始问题

I forked and customised the Google sample android-topeka, and android-studio shows an error that I don't understand about the API level required to use the method getForeground()of the class Viewin android.viewpackage:

我分叉和定制的谷歌样品Android的托皮卡,和android-Studio显示一个错误,我不明白有关API的级别要求使用方法getForeground()的类Viewandroid.view包:

Call requires API level 23 (current min is 19): android.view.View#getForeground

调用需要 API 级别 23(当前最小值为 19):android.view.View#getForeground

The referencemarks the method as added in API level 1, and the documentation (Control+Q) shows the same:

参考将方法标记为在 API 级别 1 中添加,并且文档 ( Control+Q) 显示相同:

Doc showing API level 1 method

显示 API 级别 1 方法的文档

But the Calling new methods on older versions inspectionshows an error that API level 23 is required:

但是Calling new methods on older versions inspection显示需要 API 级别 23 的错误:

Error message about API 23 required

有关需要 API 23 的错误消息

I found a similar question, a bug in that case, but I think this is not a bug due to the original fork doesn't show this error message.

我发现了一个类似的问题,在这种情况下是一个错误,但我认为这不是一个错误,因为原始 fork 没有显示此错误消息。

I'll try to detect the first time the message appears, but I did a lot of changes so any clue would be appreciated, thanks.

我会尝试检测消息第一次出现的时间,但我做了很多更改,因此我们将不胜感激,谢谢。

采纳答案by albodelu

Finally I found the related issue, setForeground()incorrectly flagged as requiring API 23 for ViewGroups extending FrameLayout.

最后我发现了相关的问题,setForeground()错误地标记为需要 API 23 来扩展FrameLayout.

Issue 189041: setForeground() incorrectly flagged as requiring API 23 (NewApi) for ViewGroups extending FrameLayout

问题 189041:setForeground() 错误地标记为需要 API 23 (NewApi) 以用于扩展 FrameLayout 的 ViewGroups

If you are not extending FrameLayout, the documentation is wrong and API 23 is really required.

如果您没有扩展FrameLayout,则文档是错误的,并且确实需要 API 23。

Issue 186273: View.setForeground wrong API level

问题 186273:View.setForeground 错误的 API 级别