Are you seeing that error in the UI editor? That kind of thing happens all the time.
Try running your code to see if it works on a device/emulator. The UI editor sometimes has problems that are not caused by your code.
If you want to fix it, you can play around with some of the settings on the UI editor preview window. The buttons at the top let you change which version of Android you’re previewing the UI with along with some other things. Changing that Android version may fix the editor’s problem.
You are right. It does work when I run in emulator or on device. I tried the various versions. Nothing worked. And then after searching a bit, adding the following in build.gradle fixed it for me. I am still starting, so do not follow what is it doing, but guess it is manually picking a certain API version for some piece of work that does not seem to impact running the application in device or emulator but fixes the UI editor