I'm going to inflate the recycler view in the home fragment using the Navigation component. There are two problems here.
- InflateException Issues
- Only one first index appears in view in ArrayList
here is error message.
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.init(HomeFragment.java:81)
at kr.hnu.project.ui.home.HomeFragment.onCreateView(HomeFragment.java:55)
And this is my code.
homefragment.java:
public class HomeFragment extends Fragment {
private final static String setMsg = "SELECT sender, receiver, title, date FROM MessageDB";
DBHelper dbHelper;
SQLiteDatabase readDB;
Cursor cursor;
ArrayList<MyItem> mailItem;
MyRecyclerAdapter myAdapter;
String curUser;
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);
NavigationActivity main = (NavigationActivity) getActivity();
curUser = main.getCurrentUserID();
dbHelper = new DBHelper(inflater.getContext());
readDB = dbHelper.getReadableDatabase();
mailItem = new ArrayList<MyItem>();
init();
myAdapter = new MyRecyclerAdapter(mailItem);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
binding.recyclerview.setLayoutManager(layoutManager); // 앞에 binding!!!
binding.recyclerview.setAdapter(myAdapter);
return root;
}
public void init() {
cursor = readDB.rawQuery(setMsg, null);
mailItem.clear();
while (cursor.moveToNext()) {
if (curUser.equals(cursor.getString(cursor.getColumnIndexOrThrow("receiver")))) {
mailItem.add(new MyItem(cursor.getString(cursor.getColumnIndexOrThrow("sender")),
cursor.getString(cursor.getColumnIndexOrThrow("title")), cursor.getString(cursor.getColumnIndexOrThrow("date"))
));
}
cursor.close();
myAdapter.notifyDataSetChanged();
}
}
fragment_home.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"/>
activity_navigation.xml:
<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>
content_navigation.xml:
<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>
You are calling
notifyDataSetChanged()
onmyAdapter
insideinit
before initializing it. That's why it's throwingNullPointerException
.Move
init()
call after you've initializedmyAdapter