After second click on Fragment, recycler view disappears

When I Click on Home navigation fragment(there is recyclerview) recycler view disappears with error message: E/RecyclerView: No adapter attached; skipping layout when the app launchs it appears but when i move to another fragment and return again to home fragment, recycler view just disappears. even If I dont move on another fragment and click on home fragment, it still disappears. What could be the problem and what can I do to fix it?

My codes: fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="1dp"
        android:paddingBottom="65dp"
        app:reverseLayout="false" />
</LinearLayout>

Posts.kt

data class Posts(
var id: Int,
var imageUrl: String,
var title: String,)

MainActivity.kt

 class MainActivity : AppCompatActivity() {

private lateinit var recyclerView: RecyclerView
private lateinit var recyclerViewPersonAdapter: RecyclerViewPersonAdapter

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val navView = findViewById<BottomNavigationView>(R.id.bottomNavMenu)
    val controller = findNavController(R.id.nav_host_fragment)
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.homeFragment,
            R.id.addFragment,
            R.id.userFragment
        )
    )
    setupActionBarWithNavController(controller, appBarConfiguration)
    navView.setupWithNavController(controller)

    recyclerView = findViewById(R.id.recyclerView)
    recyclerView.layoutManager = LinearLayoutManager(this)
    recyclerView.adapter = RecyclerViewPersonAdapter(getData())

}
private fun getData() : List<Posts>{
    val list = ArrayList<Posts>()
    list.add(
        Posts(
            1,
            "https://www.myanbiz-consulting.com/wp-content/uploads/2019/04/4214892-news-images.jpg",
            "Best news in the world\nfor you"
        )
    )
    list.add(
        Posts(
            2,
            "https://www.myanbiz-consulting.com/wp-content/uploads/2019/04/4214892-news-images.jpg",
            "very very gooood\nnews for you"
        )
    )
    list.add(
        Posts(
            3,
            "https://www.myanbiz-consulting.com/wp-content/uploads/2019/04/4214892-news-images.jpg",
            "very very gooood\nnews for you"
        )
    )
    list.add(
        Posts(
            4,
            "https://www.myanbiz-consulting.com/wp-content/uploads/2019/04/4214892-news-images.jpg",
            "very very gooood\nnews for you"
        )
    )
    return list
}

}

RecyclerViewPersonAdapter.kt

class RecyclerViewPersonAdapter(private val list: List<Posts>) : RecyclerView.Adapter<RecyclerViewPersonAdapter.PersonViewHolder>() {
    class PersonViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val imageView: ImageView

        val textView: TextView
        init {
            imageView = itemView.findViewById(R.id.imageView)
            textView = itemView.findViewById(R.id.textView)

        }
        fun setData(posts: Posts){
            Glide.with(itemView.context)
                .load(posts.imageUrl)
                .into(imageView)
            textView.text = posts.title

        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PersonViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.news_post, parent, false)
        return PersonViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: PersonViewHolder, position: Int) {
        val posts = list[position]
        holder.setData(posts)
    }

    override fun getItemCount(): Int {
        return list.size
    }
}

news_post.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="400dp"
        android:layout_height="100dp"
        android:background="@color/teal_200"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="centerCrop"
            android:src="@drawable/img" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="22dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lineSpacingExtra="8sp"
                android:text="Best news in the world\nfor you"
                android:textColor="@color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="14dp"
                        android:layout_height="14dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_baseline_thumb_up_alt_24" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:lineSpacingExtra="7sp"
                        android:text="2,687"
                        android:textColor="#333333"
                        android:textSize="13sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="22dp"
                    android:layout_weight="1"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="14dp"
                        android:layout_height="14dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_baseline_remove_red_eye_24" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:lineSpacingExtra="7sp"
                        android:text="32,577"
                        android:textColor="#333333"
                        android:textSize="13sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Solution 1:

Your RecyclerView is in fragment_home.xml. But you declare it in MainActivity.

Just do that in your HomeFragment instead of MainActivity.

class HomeFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    recyclerView = view.findViewById(R.id.recyclerView)
    recyclerView.layoutManager = LinearLayoutManager(requireContext())
    recyclerView.adapter = RecyclerViewPersonAdapter(getData())
        ...