“调用需要 API 级别 23"错误,但 API 1 的 FrameLayout 上存在 getForeground() [英] 'Call requires API level 23' error, but getForeground() exists on FrameLayout from API 1
问题描述
总结
最后,我发现了相关问题,setForeground()
在扩展 FrameLayout
的 ViewGroups 上被错误地标记为需要 API 23.
新问题
所以我的问题变成了如何使用以前版本中的方法,以及,为什么会选择性地出现检查错误?
如果我添加一个多余的演员,错误信息就会消失:
((FrameLayout) layout).getForeground()
但是它正在访问现在不支持的 View
方法.
因此,我认为针对以前的 API 会删除错误消息,但我很感激使用此 Framelayout 方法的解决方案或解决我的问题的线索:
ANDROID_COMPILE_SDK_VERSION=23ANDROID_BUILD_TOOLS_VERSION=23.0.1ANDROID_DEFAULT_MIN_SDK_VERSION=19ANDROID_DEFAULT_TARGET_SDK_VERSION=23
解决方法
简而言之,作为一种解决方法,我直接将类型更改为 FrameLayout
,因此关于 API 和强制转换的警告消失了,我测试它在 Android 4.4.4 设备和调试上工作,我想它会崩溃,反正它只用在一个动画中,我将来会修复/替换它.
初始问题
我分叉并定制了 Google 示例
但是在旧版本检查中调用新方法
显示需要API级别23的错误:
我发现了一个类似的问题,这是一个错误那种情况,但我认为这不是错误,因为原始 fork 没有显示此错误消息.
我会尝试检测消息第一次出现的时间,但我做了很多更改,因此我们不胜感激,谢谢.
最后我发现了相关问题,setForeground()
错误地标记为需要 API 23 for ViewGroups extends FrameLayout
>.
问题 189041:setForeground() 被错误地标记为需要 API 23 (NewApi) 以用于 ViewGroups 扩展 FrameLayout
如果您没有扩展 FrameLayout
,则文档是错误的,并且确实需要 API 23.
问题 186273:View.setForeground API 级别错误
Summary
Finally, I found the related issue, setForeground()
incorrectly flagged as requiring API 23 on ViewGroups extending FrameLayout
.
If you are not extending FrameLayout
, the documentation is wrong and API 23 is required.
Issue 186273: View.setForeground wrong API level
Updated question
Marked as duplicated of Can't use setForeground
method on ImageView by Commonsware and answered by him:
That is a documentation bug. setForeground() existed on FrameLayout from API Level 1; it is only on View as of API Level 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.
But It's not the same case, this code is already extending a Framelayout
and 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.
Update: I add a diff report for FrameLayout:
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 View
method that is not supported now.
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:
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 FrameLayout
directly, 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.
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 View
in android.view
package:
Call requires API level 23 (current min is 19): android.view.View#getForeground
The reference marks the method as added in API level 1, and the documentation (Control+Q
) shows the same:
But the Calling new methods on older versions inspection
shows an error that API level 23 is required:
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.
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.
Finally I found the related issue, setForeground()
incorrectly flagged as requiring API 23 for ViewGroups extending FrameLayout
.
If you are not extending FrameLayout
, the documentation is wrong and API 23 is really required.
Issue 186273: View.setForeground wrong API level
这篇关于“调用需要 API 级别 23"错误,但 API 1 的 FrameLayout 上存在 getForeground()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!