Android data binding in fragment - IllegalArgumentException: No view found

Viewed 7290

I have a fragment that contains a recyclerview. This recyclerview is bound to my viewmodel using data binding.

Problem; I get the following exception trying to launch the fragment:

FATAL EXCEPTION: main
Process: no.inforte.demo, PID: 3134
    java.lang.IllegalArgumentException: No view found for id 0x7f0d0074 (no.inforte.demo:id/content_frame) for fragment CategoryListFragment2{6db9671 #1 id=0x7f0d0074 CATEGORYLISTFRAGMENT}
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
    ...
    ...

The error states correctly that the 'content_frame' does not exist. This view is in the layout for the MainActivity.java class, as my fragment container.

Since I am using data binding I use the DataBindingUtil.setContentView method:

private void initDataBinding() {
    Log.d(TAG, "initDataBinding");
    mCategoryListFragmentBinding = DataBindingUtil.setContentView(getActivity(), R.layout.category_list_fragment);
    mCategoryListViewModel = new CategoryListViewModel(mCategoryListView, getContext());
    mCategoryListFragmentBinding.setCategoryListViewModel(mCategoryListViewModel);
}

How to resolve?

More info:

This is how I launch this fragment from the MainActivity:

CategoryListFragment2 categoryListFragment = new CategoryListFragment2();
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame, categoryListFragment, FRAGMENT_TAG_CATEGORYLIST)
            .commit();
getSupportFragmentManager().executePendingTransactions();

This is my fragment layout:

<?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"
    tools:context="no.inforte.demo.ui.fragment.CategoryListFragment2">

    <data>
        <variable
            name="categoryListViewModel"
            type="no.inforte.demo.ui.viewmodel.CategoryListViewModel" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/category_list"
                android:name="no.inforte.demo.ui.fragment.CategoryListFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="50dp"
                android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:layout_marginRight="@dimen/activity_horizontal_margin"
                app:layoutManager="LinearLayoutManager"
                tools:context="no.inforte.demo.ui.fragment.CategoryListFragment"
                tools:listitem="@layout/rowlayout_category_list">

        </android.support.v7.widget.RecyclerView>
    </RelativeLayout>
</layout>
2 Answers

I also ran into this issue and found the solution using DataBindingUtil.inflate method. Everything you did is correct except your initDataBinding method should be removed and replaced as follows:

In your fragment class (the one that extends Fragment), you need to override the onCreateView method and do this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    CategoryListFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.category_list_fragment , container, false);
    binding.setViewModel(new CategoryListViewModel());
    return binding.getRoot();
}

Once you do that your data binding will work properly and you are good to go!

Related