Animax
Animax

Reputation: 1

Recycler View not accessible (Android Studio)

I put a recycler view in my xml and gave it an id. Then I went to the main activity to call the id, but it did not appear. I cleaned the project and rebuilt. I closed the project and opened it again, could not find it, and then I tried to use binding. It did find the id but it gave me this error:

Cannot access 'no_name_in_PSI_3d19d79d_1ba9_4cd0_b7f5_b46aa3cd5d40' which is a supertype of 'com.example.recyclervew.databinding.ActivityMainBinding'. Check your module classpath for missing or conflicting dependencies

I even tried using the beta version but same error.

package com.example.recyclervew

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.recyclervew.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        binding.recyclerView
    }
}

IDE Screenshot

I would appreciate any suggestions from the community. Thanks.

Upvotes: 0

Views: 154

Answers (2)

Amit pandey
Amit pandey

Reputation: 1195

set data binding enable in your gradle file \

 dataBinding {
    enabled = true
}

and than do set binding like this

private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
   

    binding.recyclerView
}

Upvotes: 1

Teo
Teo

Reputation: 982

did you enable databinding in build.gradle(app)? Something like this

build.gradle(app)

android{
  buildFeatures {
        viewBinding = true
        dataBinding = true
    }
}

and also, please inflate databinding first before you use them.

private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(R.layout.activity_main)

        binding.recyclerView
    }

Upvotes: 2

Related Questions