Can't get Root View from Data Binding after enabling safe-args plugin
Asked Answered
M

5

16

I'm working on an Android app using dataBinding and am currently trying to add the safe-args plugin, but after enabling the plugin, I can no longer get the root view via binding.root - Android Studio gives the error:

Unresolved Reference
None of the following candidates is applicable because of a receiver type mismatch:
* internal val File.root: File defined in kotlin.io

How can I get databinding and safe-args to play nice together?

Note that while the code snippet is in Kotlin I will happily take Java answers. Not as comfortable in Java but I can easily read it and translate it.

I haven't been able to find anyone else with the same problem by Googling the error message and "safe args". I tried first with the classpath listed in the Android docs here: https://developer.android.com/guide/navigation/navigation-pass-data

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0"

And then also found a tutorial suggesting I use:

classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha07"

Both had the same issue: binding.root gave an error with the plugin activated

Here is my onCreateView() for my fragment. That return line works properly when safe-args isn't enabled and doesn't work when it is enabled

   override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_encoder, container, false)

    return binding.root
}

Any help solving or understanding this problem is most appreciated!

Message answered 23/10, 2019 at 5:55 Comment(0)
K
16

I have the same issue, it's so wired for me, but just rename the layout will work again, try to it :D

Keeleykeelhaul answered 25/10, 2019 at 4:32 Comment(4)
That did do the trick, strangely enough, thanks. For others coming here with the same problem, all I had to do was refactor>rename the layout xml file associated with the class that wasn't recognizing binding.root. You'll also have to rename the auto-generated class type for the binding accordingly. Once I did that, everything worked correctly and I was even able to re-rename back to the original name. Not sure how this did anything rebuilding the project didn't..Message
Also if you're here due to issues with safe-args, might wanna look into using a shared ViewModel and LiveData instead. That's what I'm using now cause I had to keep going when I got stuck on safe-args and I'm much happierMessage
invalid caches command didn't work but renaming the layout which was not recognised did the trick.Braswell
Looks like if an ID inside the layout matches one of the names it gets thrown off, renaming the layout to match the view fixed it - weird!Astilbe
F
24

I have the same issue and at last I tried

File -> Invalid Caches/Restart

It works for me.

Ferryman answered 26/10, 2019 at 9:8 Comment(3)
Just Invalidate works, no need to restart Android Studio.Guiscard
Didn't work for me. Only renaming the layout file worked!Arsenical
@RahulRastogi wow, nothing worked except your solution, which is to rename the file, how did you come up with that solution? :)Coffle
K
16

I have the same issue, it's so wired for me, but just rename the layout will work again, try to it :D

Keeleykeelhaul answered 25/10, 2019 at 4:32 Comment(4)
That did do the trick, strangely enough, thanks. For others coming here with the same problem, all I had to do was refactor>rename the layout xml file associated with the class that wasn't recognizing binding.root. You'll also have to rename the auto-generated class type for the binding accordingly. Once I did that, everything worked correctly and I was even able to re-rename back to the original name. Not sure how this did anything rebuilding the project didn't..Message
Also if you're here due to issues with safe-args, might wanna look into using a shared ViewModel and LiveData instead. That's what I'm using now cause I had to keep going when I got stuck on safe-args and I'm much happierMessage
invalid caches command didn't work but renaming the layout which was not recognised did the trick.Braswell
Looks like if an ID inside the layout matches one of the names it gets thrown off, renaming the layout to match the view fixed it - weird!Astilbe
N
0

I have the similar issue but only in included layout, perform these operations work for me

Build -> Clean Project
Build -> Rebuild Project
Nucleotidase answered 30/3, 2021 at 4:33 Comment(0)
O
0

I've already tried 「Invalidate Caches」、「Clean & Build」、「Rebuild」、Restart PC. However,They all did not work.

Until I did these:

  1. Clean
  2. remove 「.gradle」 & 「.buildCacheDir」
  3. build

then it worked. Oh My God! try it!

Orthoclase answered 30/6, 2022 at 7:45 Comment(0)
Y
0

After I spend hours and tried everything, I remove .gradle/.tmp and .gradle/caches files completely and the problem disappeared

Yost answered 22/3, 2023 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.