Search code examples
javaandroidxmlandroid-recyclerviewretrofit

Binary XML file has no LayoutManager


This is the error I'm getting:

Process: com.tunedapps.finalproject, PID: 18290
    android.view.InflateException: Binary XML file line #7 in com.tunedapps.finalproject:layout/news_deatils_fragment: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{ada8e19 VFED..... ......I. 0,0-0,0 #7f090157 app:id/news_container}, adapter:null, layout:null, context:com.tunedapps.finalproject.base.BaseActivity@136b5d
    Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{ada8e19 VFED..... ......I. 0,0-0,0 #7f090157 app:id/news_container}, adapter:null, layout:null, context:com.tunedapps.finalproject.base.BaseActivity@136b5d

this is the block of code where I'm setting the adapter and layoutManager:

public void onResponse(Call<List<Data>> call, Response<List<Data>> response) {


                    newsList = response.body();
                    LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.this);

                    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.news_container);
                    recyclerView.setLayoutManager(layoutManager);

                    NewsAdapter recyclerViewAdapter = new NewsAdapter(getApplicationContext(), newsList);
                    recyclerView.setAdapter(recyclerViewAdapter);
                }

this is the inflator that's having the error:

View rootView = inflater.inflate(R.layout.news_deatils_fragment, container, false);

this is the new_container XML code:

Is alongside an appBarLayout which are inside a RelativeLayout which is inside a DrawerLayout.

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/news_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize" />

this is the news_deatils_fragment code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:id="@+id/rv"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Please remove app:layoutManager from constraint layout

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager">
    
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            android:id="@+id/rv"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>