'Error inflating Class Fragment in Recyclerview with Navigation Activity

I'm making an app that uses Android studio to write and call messages. This error message appears during the process of inflating the recycler view into the fragment.

How can I fix it? Why can't this be inflated?

here is error messages...

java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.hnu.project/kr.hnu.project.NavigationActivity}: android.view.InflateException: Binary XML file line #11 in kr.hnu.project:layout/activity_navigation: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.view.InflateException: Binary XML file line #11 in kr.hnu.project:layout/activity_navigation: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void kr.hnu.project.MyRecyclerAdapter.notifyDataSetChanged()' on a null object reference
        at kr.hnu.project.ui.home.HomeFragment.setArray(HomeFragment.java:83)
        at kr.hnu.project.ui.home.HomeFragment.onCreateView(HomeFragment.java:52)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)

and this HomeFragment that the recycler view should see.

public class HomeFragment extends Fragment {
private final static String selectMsg = "SELECT sender, receiver, title, date, content FROM MessageDB";
RelativeLayout rel;
LinearLayout lin;
DBHelper dbHelper;
SQLiteDatabase readDB;
Cursor cursor;
ArrayList<MyItem> mailItem;
MyRecyclerAdapter myAdapter;

private FragmentHomeBinding binding;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    HomeViewModel homeViewModel =
            new ViewModelProvider(this).get(HomeViewModel.class);

    binding = FragmentHomeBinding.inflate(inflater, container, false);
    View root = binding.getRoot();
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_home, container, false);

    dbHelper = new DBHelper(inflater.getContext());
    readDB = dbHelper.getReadableDatabase();
    mailItem = new ArrayList<MyItem>();

    setArray();

    myAdapter = new MyRecyclerAdapter(getActivity(), mailItem);
    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(myAdapter);




    return root;
}

public void setArray() {
    cursor = readDB.rawQuery(selectMsg, null);
    mailItem.clear();

    while (cursor.moveToNext()) {

        mailItem.add(new MyItem(cursor.getString(cursor.getColumnIndexOrThrow("sender")), cursor.getString(cursor.getColumnIndexOrThrow("receiver")),
                cursor.getString(cursor.getColumnIndexOrThrow("title")), cursor.getString(cursor.getColumnIndexOrThrow("date")),
                cursor.getString(cursor.getColumnIndexOrThrow("content"))));

    }

    cursor.close();
   //myAdapter.notifyDataSetChanged();

}

This is Navigation Activity with home fragment.

public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private AppBarConfiguration mAppBarConfiguration;
private ActivityNavigationBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    dataToFragment(); 

    binding = ActivityNavigationBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());


    setSupportActionBar(binding.appBarNavigation.toolbar); 

    DrawerLayout drawer = binding.drawerLayout;
    NavigationView navigationView = binding.navView;
    navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this); 

    //
    mAppBarConfiguration = new AppBarConfiguration.Builder( 
            R.id.nav_send, R.id.nav_set, R.id.nav_home)
            .setOpenableLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_navigation);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

this is activity_navigation.xml in error message

 <?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    android:id="@+id/app_bar_navigation"
    layout="@layout/app_bar_navigation"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />



<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_navigation"
    app:menu="@menu/activity_navigation_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

this is content_navigation.xml in error message

<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_navigation">

<fragment
    android:id="@+id/nav_host_fragment_content_navigation"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" /

</androidx.constraintlayout.widget.ConstraintLayout>

and this is homeFragment's xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.home.HomeFragment">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>


Solution 1:[1]

because my reputation is less then 50 ,I can't comment on your post so i will comment here .

What does this method do? dataToFragment(); you are calling it before inflating the layout

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 just developer