Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Summary

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

Issue 189041: setForeground() incorrectly flagged as requiring API 23 (NewApi) for 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:

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 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:

Doc showing API level 1 method

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

Error message about API 23 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.

like image 471
albodelu Avatar asked Oct 27 '15 22:10

albodelu


1 Answers

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

Issue 189041: setForeground() incorrectly flagged as requiring API 23 (NewApi) 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

like image 183
albodelu Avatar answered Sep 30 '22 13:09

albodelu