Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 856
Hannah S. Avatar asked Oct 23 '19 05:10

Hannah S.


People also ask

How would you enable view binding for a module in your application so you can reference views from layout files without using findViewById?

To enable view binding, configure viewBinding in your module-level build. gradle file. Once enabled for a project, view binding will generate a binding class for all of your layouts automatically. You don't have to make changes to your XML — it'll automatically work with your existing layouts.

What does the safe args gradle plugin do?

The Navigation component has a Gradle plugin called Safe Args that generates simple object and builder classes for type-safe navigation and access to any associated arguments. Safe Args is strongly recommended for navigating and passing data, because it ensures type-safety.

What is ViewBinding?

View binding allows a developer to incorporate significant interaction in an application. This concept seeks to eliminate the findViewById keyword. Removing such boilerplate code allows developers to be more productive. A binding class is usually generated for each layout file when using view binding.


2 Answers

I have the same issue and at last I tried

File -> Invalid Caches/Restart

It works for me.

like image 134
Daniel Chin Avatar answered Nov 18 '22 03:11

Daniel Chin


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

like image 15
MarkWang Avatar answered Nov 18 '22 03:11

MarkWang