0

I try to implement the following fragment in my app.

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jiangdg.ausbc.base.CameraFragment
import com.jiangdg.ausbc.widget.AspectRatioTextureView
import com.jiangdg.ausbc.widget.IAspectRatio

class DemoFragment : CameraFragment() {
    private lateinit var mViewBinding: DemoFragmentBinding

    override fun initView() {
        super.initView()
    }

    override fun getCameraView(): IAspectRatio {
        return AspectRatioTextureView(requireContext())
    }

    override fun initData() {
        super.initData()
    }

    override fun getCameraViewContainer(): ViewGroup {
        return mViewBinding.cameraViewContainer
    }

    override fun getRootView(inflater: LayoutInflater, container: ViewGroup?): View {
        mViewBinding = DemoFragmentBinding.inflate(inflater, container, false)
        return mViewBinding.root
    }

    override fun getGravity(): Int = Gravity.TOP
}

but the DemoFragmentBinding is not recognized by android studio: I get the following error :

Unresolved reference: DemoFragmentBinding

I think the problem is coming from the XML, I tried with the one on the android Docs

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" >
    <TextView android:id="@+id/DemoFragment" />
    <ImageView android:cropToPadding="true" />
</LinearLayout>

But it doesn't change anything

I tried several xml found over the internet but no of them worked. I'm not sure if the xml is the problem, and my question may be dumb, but can someone give me a lead to the good direction?

Edit: I forgot to explain: I have already put this in my app gradle

android {
    buildFeatures {
        dataBinding true
        viewBinding = true
    }
Clamore
  • 15
  • 1
  • 5

1 Answers1

0

Firstly:

android {
    buildFeatures {
        dataBinding true
        viewBinding = true
    }

This is not correct, viewBinding is a part of dataBinding, so enabling "both" (separately) at the same time is... strange, judging on your title you want to use viewBinding only, so change the code above to:

android {
    buildFeatures {
        viewBinding true
    }

Then, if you sync your project, clean and rebuild it you should be able to reference binding variables inside of your fragments / activity (sometimes the Binding variables are not created after the build, that's why you would need to clean and rebuild))

JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
  • I tried your solution(then clean project and rebuild) but it doesn't seem to change anything, the problem is still here but thanks for the precision. – Clamore Nov 16 '22 at 08:30
  • Are you sure the binding names you are referencing are correct? The binding variables take the name after the XML files, NOT the classes, so "fragment_home.xml" would create a variable named "FragmentHomeBinding", even if the class is named "HomeFragment.kt" (The binding variables should definitevely be created by now, so I cannot see any other reason) – JustSightseeing Nov 16 '22 at 10:59
  • my xml file is named demo_fragment.xml and is in my res/xml dir. and I call my variable DemoFragmentBinding. in my file which is called DemoFragment.kt . Do you see any problem with these name? – Clamore Nov 16 '22 at 13:48
  • I just created a new project, enabled viewBinding, created a fragment "demo_fragment.xml" (with a DemoFragment kotlin class) and I'm able to reference "DemoFragmentBinding" after syncing my project. Maybe try to remove that fragment and create it again? – JustSightseeing Nov 16 '22 at 13:57
  • Also, just to be sure, you enabled "viewBinding", not "dataBinding" right? – JustSightseeing Nov 16 '22 at 13:59
  • yep viewBinding = true in my app build graddle – Clamore Nov 16 '22 at 14:11
  • where do you put your demo_fragment.xml ? maybe I just place it in the wrong path – Clamore Nov 16 '22 at 14:13
  • res -> layout, also I don't think the path is the problem here, viewBinding should work for you It might be some kind of internal error (starting a new project and enabling viewBinding in the beginning would definitevely fix that, but that's not the best solution) – JustSightseeing Nov 16 '22 at 14:16
  • 1
    I moved it to res -> layout and it worked perfectly thanks a lot, you saved me – Clamore Nov 16 '22 at 14:53
  • oh, well, then I learned something too! :) – JustSightseeing Nov 16 '22 at 14:54