5

I am developing a single activity application using the Android Jetpack navigation component. On one of the fragments I utilize the built-in data binding tools. Strangely enough, even though it was working just the week before, it just completely broke today for seemingly no reason.

The setup:
The fragment I use with binding has the following layout file:

<?xml version="1.0" encoding="utf-8"?>
<layout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/example_layout_root"
        tools:context=".example.ExampleFragment"
        android:background="@color/main_1"
        >

        ...

    </ScrollView>
</layout>

I have stripped the main content but it shows that I have a <layout> element as the root with both the data and the fragment layout part defined.

The fragment code is the following:

class ExamleFragment : Fragment() {

    private val viewModel: ExampleViewModel by sharedViewModel()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        val binding : FragmentExampleBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_example, container, false)
        // This one also does not work
        // val binding = FragmentExampleBinding.inflate(inflater, container, false)
        binding.vm = viewModel
        binding.lifecycleOwner = this

        return binding.root
    }
}

I use Koin to inject the viewmodel into the fragment.
When I try to inflate the layout (either with DataBindingUtil or the generated FragmentExampleBinding class) it results in the following exception:

java.lang.IllegalStateException: FragmentExampleBin…flater, container, false) must not be null at com.example.fragments.ExampleFragment.onCreateView(ExampleFragment.kt:38) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698) at ...

And the stack goes straight down into OS territory so basically it doesn't give any useful information. I can't figure out why the result is null.

What I have tried:

  • I have tried inflating the layout with inflater.inflate(...) and it works just fine but that way I can't use the data bindings.

  • I have tried removing EVERYTHING from the layout description that's not absolutely neccessary and it still does not work.

  • I have tried reverting to previous commits (as far back as the first working version with binding) and even though it used to work before, it does not work now.

  • I even restarted and reset the emulator, tried different images, but to no avail.

Has anyone encountered anything similar to this? The code really 'broke' over the weekend as I have just tested it the Friday before and it worked properly.

EDIT:
Okay, so I somehow managed to fix it.
I started experimenting with creating another fragment with data binding to see whether things are broken for all fragments or just that specific one. The new one seemed to work just fine but strangely, the old, broken fragment still refused to work even though they were basically the same.
So I did Clean + Rebuild (which I swear I performed as a first attempt to fix the issue) and it kind of just fixed it.

2
  • 1
    Holy f'kin cow. How much ever times I did clean+rebuild, invalidate cache, it didn't work. I jut renamed the xml and the class name to something else, and things started working fine. What BS is this.
    – Vik
    Dec 18, 2020 at 8:53
  • The solution of @Vik worked for me... I don't have a multi-module project and renaming the xml + class worked.
    – Z-100
    May 5, 2022 at 12:11

4 Answers 4

7

In my multi-module project, the reason for this problem was the fact that there was some kind of package name clash.

If you are getting this error without an apparent reason, double check the AndroidManifest.xml files of your data binding enabled modules.

Making sure all of the data binding enabled modules use unique package names solved the problem for me.

I spent hours to find the root of this problem hopefully others read this answer and won't spend that much.

1
  • 2
    Nowadays, also have a look at build.gradle android { namespace } which also causes the same problem. Apr 27 at 6:50
0

Dont use DataBindingUtil for fragments, run your app once and android studio will generate a binding class for that fragment based off the name of your xml file, in your case the xml file is call fragment_example, so the class generated will be FragmentExampleBinding, then you call FragmentExampleBinding.inflate(....) like this:

val binding = FragmentExampleBinding.inflate(inflater, container, false);
3
  • 1
    I have tried inflating as such but sadly it still results in a null object being inflated. What I don't understand is that it used to work just fine a few days before with the exact same code. Jan 21, 2020 at 9:06
  • It could be an issue with your layout file, it could be throwing an exception during inflation are there any logs? Jan 24, 2020 at 8:58
  • In my case, inflating directly using the generated class' name works. But DataBindingUtil.inflate returns nulls. And it's an activity Oct 24 at 7:18
0

Do it this way :

class ExamleFragment : Fragment() {

        private val viewModel: ExampleViewModel by sharedViewModel()
        lateinit binding:FragmentExmaple

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            // Inflate the layout for this fragment
             binding = DataBindingUtil.inflate(inflater, R.layout.fragment_example, container, false)

            binding.vm = viewModel
            binding.lifecycleOwner = this

            return binding.root
        }
    }

please I need you to paste your error here

5
  • i think it might be an issue with koin can you kindly help me by sending your error log Jan 20, 2020 at 15:20
  • The error I get is the following: java.lang.IllegalStateException: DataBindingUtil.inflate(…, container, false) must not be null. It's because I'm assigning the resulting null object to a non-nullable field. The root cause is the same: the inflation results in a null object and I can't figure out why. Jan 20, 2020 at 15:30
  • I have edited the post with the exception stack trace. Cut the part of it that's just OS code because it's not really relevant to my problem. Jan 21, 2020 at 9:18
  • 1
    this happends when root is not from an inflated binding layout.what you should do is delete your lateinit binding:FragmentExmaple remove any import related to binding go to your layout delete the layout tags right click convert to databinding and rebuild and add back your lateinit binding:FragmentExmaple it should do the magic Jan 21, 2020 at 14:27
  • I don't really understand what happened, but after reverting to the previously working commit, then running Clean + Rebuild several times, it just... fixed itself? I guess it was some generated code that got messed up, but I'm not sure. Jan 22, 2020 at 9:11
0

in my case I just forgot to add @AndroidEntryPoint to my fragment class

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.