Alon Shlider
Alon Shlider

Reputation: 1298

Error trying to generate Binding class for my Fragment

enter image description here

I have the following XML file which is a layout for my Fragment -

<?xml version="1.0" encoding="utf-8"?>

<layout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical"
        android:padding="15dp"
        tools:context=".fragments.DashboardFragment">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/fragment_dashboard_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</layout>

The FragmentDashboardBinding class is indeed auto-generated, but I can't use it -

class DashboardFragment : Fragment(R.layout.fragment_dashboard) {

    private lateinit var binding : FragmentDashboardBinding


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = FragmentDashboardBinding.inflate(inflater, container, false)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

}

enter image description here

I get the error I picured at the top when trying to build the project, making it unusable

I am missing something. What could it be ?

I have wrapped my layout file with as I should

I currently only need View Binding, and not data binding.

Upvotes: 1

Views: 318

Answers (1)

Shalu T D
Shalu T D

Reputation: 4039

Please change your fragment as below:

class DashboardFragment : Fragment() {

    private lateinit var binding : FragmentDashboardBinding

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

}

`

Upvotes: 1

Related Questions