Home > Back-end >  RecyclerView has no LayoutManager | Kotlin
RecyclerView has no LayoutManager | Kotlin

Time:10-12

I am trying to create a CardView within a RecyclerView but I can't get it to launch since the app crashes after starting with this error "RecyclerView has no LayoutManager". I've tried to apply fixes suggested by other posts regarding the same problem but got no luck so far.

Thank you in advance.

HomeActivity.kt

class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home)

    // Retrieves data from source
    val arrayList = ArrayList<PreviewCardModel>()
    arrayList.add(PreviewCardModel("Product A","500mg / 1000mg", R.drawable.image, 500))
            arrayList.add(PreviewCardModel("Product D","Sample", R.drawable.image, 5040))
            arrayList.add(PreviewCardModel("Product E","Sample", R.drawable.image, 5500))
            arrayList.add(PreviewCardModel("Product F","Sample", R.drawable.image, 2500))

    val myAdapter = CardAdapter(arrayList, this)
    recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false)
    recyclerView.adapter = myAdapter

CardAdapter.kt

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.sample.appname.R
import com.sample.appname.model.PreviewCardModel
import kotlinx.android.synthetic.main.preview_card.view.*

class CardAdapter (private val arrayList: ArrayList<PreviewCardModel>, val context: Context) :
    RecyclerView.Adapter<CardAdapter.CardViewHolder>() {

    class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindItems (model: PreviewCardModel) {
            itemView.previewName.text = model.title
            itemView.previewDesc.text = model.desc
            itemView.previewPrice.text = model.price.toString()
            itemView.previewImage.setImageResource(model.image)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {

        val view = LayoutInflater.from(parent.context).inflate(R.layout.preview_card, parent, false)

        return CardViewHolder(view)
    }

    // Returns size of data list
    override fun getItemCount(): Int {
        return arrayList.size
    }
    // Displays data at a certain position
    override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
        holder.bindItems(arrayList[position])
        }
    }

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".HomeActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@color/browser_actions_title_color"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:orientation="horizontal"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

This is the error I am encountering.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sample.appname, PID: 5333
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.appname/com.sample.appname.HomeActivity}: android.view.InflateException: Binary XML file line #54 in com.sample.appname:layout/activity_home: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{b2c2d71 VFED..... ......I. 0,0-0,0 #7f09016f app:id/recyclerView}, adapter:null, layout:null, context:com.sample.appname.HomeActivity@5dcb507

preview_card.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="120dp"
    android:padding="10sp"
    app:cardCornerRadius="3dp"
    app:cardElevation="3dp"
    app:cardUseCompatPadding="true">

        <ImageView
            android:id="@ id/previewImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:src="@drawable/heart"
            android:contentDescription="@string/product_image" />

        <TextView
            android:id="@ id/previewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/product_name"
            android:textSize="11sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/previewImage" />

        <TextView
            android:id="@ id/previewDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SAMPLE"
            android:textSize="8sp"
            android:textColor="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/previewName"
            tools:ignore="SmallSp" />

        <TextView
            android:id="@ id/previewPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="@string/samplePrice"
            android:textColor="@color/colorPrimary"
            android:textSize="11sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />

</androidx.cardview.widget.CardView>

CodePudding user response:

This error occurs when you've created same layout for multiple SDK or orientation and not mentioned the view in all the layout files, say for: activity_main.xml(v21)or activity_main(land) and so on. All the layouts should contain all the views with same IDs.

FIX: I had another activity_home(v21) layout that was cause the problems. Make sure you don't have multiple layouts that will cause problems. It was detecting no LayoutManager in the v21 version layout.

  • Related