I am making my first app in android studio. So far I have built a bottom navigation bar using fragments. I am now trying to add a calendar feature to one of the fragments, I have copied a tutorial using activities to run the code and think there is a problem with using "View".
This is the code to open for the nav bar to open the fragment.
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:
getSupportFragmentManager().beginTransaction().replace(R.id.container,homeFragment).commit();
return true;
case R.id.calendar:
getSupportFragmentManager().beginTransaction().replace(R.id.container,calendarFragment).commit();
return true;
case R.id.wellbeing:
getSupportFragmentManager().beginTransaction().replace(R.id.container,wellbeingFragment).commit();
return true;
}
return false;
This is java in my calendar fragment, which when opened from the navigation bar crashes the app
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
initWidgets();
selectedDate = LocalDate.now();
setMonthView();
return inflater.inflate(R.layout.fragment_calendar, container, false);
}
private void initWidgets()
{
calendarRecyclerView = requireView().findViewById(R.id.calendarRecyclerView);
monthYearText = requireView().findViewById(R.id.monthYearTV);
}
private void setMonthView()
{
monthYearText.setText(monthYearFromDate(selectedDate));
ArrayList<String> daysInMonth = daysInMonthArray(selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
}
This is the error message I receive in my logcat
2022-05-12 12:45:05.305 10950-10950/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 10950
java.lang.IllegalStateException: Fragment CalendarFragment{c4de180} (94b1c782-ca39-403f-b45b-5d0d7a042f15 id=0x7f080087) did not return a View from onCreateView() or this was called before onCreateView().
at androidx.fragment.app.Fragment.requireView(Fragment.java:1964)
at com.example.myapplication.CalendarFragment.initWidgets(CalendarFragment.java:41)
at com.example.myapplication.CalendarFragment.onCreateView(CalendarFragment.java:30)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
I believe there is no error in the calendar code and is something to do with onCreateView
in the fragment code.
CodePudding user response:
You cannot call requireView()
before returning a non-null value from onCreateView()
.
Move these lines
initWidgets();
selectedDate = LocalDate.now();
setMonthView();
(where initWidgets()
calls requireView()
) from onCreateView()
to e.g. onViewCreated()
.
CodePudding user response:
Yes it crashes because findViewById wasn't called from the fragment_calendar layout before returning inflater therefore anything called after the return statement will not be executed and will return a NullPointerException. Unless you override onViewCreated method and put down the codes
So to avoid that, you need to override onViewCreated method or you must return view. Remember, if you want to return view then all codes or public methods must be reached before the return view statement. Else, it'll crash due to exception
Check this codes, it'll fix it correctly
public class TestFragment extends Fragment {
RecyclerView calendarRecyclerView;
TextView monthYearText;
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
selectedDate = LocalDate.now();
return inflater.inflate(R.layout.fragment_calendar, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initWidgets(view);
setMonthView();
}
private void initWidgets(View view) {
calendarRecyclerView = view.findViewById(R.id.calendarRecyclerView);
monthYearText = view.findViewById(R.id.monthYearTV);
}
private void setMonthView() {
monthYearText.setText(monthYearFromDate(selectedDate));
ArrayList<String> daysInMonth = daysInMonthArray(selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, getActivity());
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarAdapter.setAdapter(calendarAdapter);
}
}