0

I am created a fragment and I want to insert it into a framelayout which is inside the content of my cardview. Inside the fragment there is another reyclerView.

View view;
private List<DayViewItem> dayItemList = null;

public static Day_Fragment newInstance() {
    // Required empty public constructor
    Day_Fragment day_fragment = new Day_Fragment();
    return day_fragment;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_day_, container, false);

    initializeDayItemList();

    //Create recyclerview
    RecyclerView dayRecyclerView = view.findViewById(R.id.day_recycler_list);

    // Create the grid layout manager with 2 columns
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 1);

    // Set layout manager.
    dayRecyclerView.setLayoutManager(gridLayoutManager);

    // Create recycler view data adapter with item list.
    DayViewDataAdapter dayDataAdapter = new DayViewDataAdapter(dayItemList);
    // set data adapter
    dayRecyclerView.setAdapter(dayDataAdapter);

    return view;
}

private void initializeDayItemList() {

    if (dayItemList == null)
    {
        dayItemList = new ArrayList<DayViewItem>();

        dayItemList.add(new DayViewItem("Blade Replacement","Normal", "Done"));
        dayItemList.add(new DayViewItem("Blade Replacement","Daily", "Done"));
        dayItemList.add(new DayViewItem("Device Change Setup Checklist","Normal", "Cancel"));
        dayItemList.add(new DayViewItem("Device Change Setup Checklist","Daily", "Cancel"));
    }
}

I insert the fragment in MainActivity

Fragment DayFragment = new Day_Fragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
            .replace(R.id.day_container, ((Day_Fragment) DayFragment).newInstance())
    .commit();

I having this error:

java.lang.IllegalArgumentException: No view found for id 0x7f08003d (com.example.nestedrecyclerview:id/day_container) for fragment Day_Fragment{2b9a2b0 #0 id=0x7f08003d}
  • 1
    Where is `day_container` exactly? That is, which layout is it in, and where is that layout being used? – Mike M. Aug 23 '19 at 04:15
  • day_container is in the RecyclerviewItem layout which is in a cardview. I am using recyclerview to generate my list of cardview. Inside my cardview i want to insert another fragment that is also a RecyclerView – Reuben Kai Min Aug 23 '19 at 05:31
  • `Fragment`s generally aren't used in `RecyclerView` items. It is possible, but it's cumbersome, and error-prone. Does the outer `RecyclerView` really need to be a `RecyclerView`? For example, it's relatively easy to use `Fragment`s with a `ViewPager`. Can you use that instead? – Mike M. Aug 23 '19 at 05:39
  • I am doing this way because I wanted to create a nested recyclerview with cardview – Reuben Kai Min Aug 23 '19 at 05:43
  • Right, but your desired setup is not going to be as straightforward as using a `Fragment` directly in an `Activity`. I'm trying to find an easier solution for you. Can you use a `ViewPager` instead? Also, I see that your `dayItemList` only has four items in it. Is it only ever going to have four items? If so, then you don't really need a `RecyclerView`, and you wouldn't need the `Fragment` to manage it. You could just make a regular `View` to put in the `Activity`'s `RecyclerView` with those four items. – Mike M. Aug 23 '19 at 05:51
  • no the 4 items is just doing testing, item data will come from the database, thus i need recyclerview to display it. I will try to do it in viewpager – Reuben Kai Min Aug 23 '19 at 05:57

2 Answers2

0

The R.id.day_container is not found, and this can be due to a variety of things. For instance, loading different resources based on language or device resolution, and this id not being found in the resource that has been loaded, or accessing an R file that does not contain this resource.

  • Could you double-check that the resource you are loading contains this particular ID? (day_container)
  • Could you use the full qualified name on the R resource? (your.package.R.id.day_container)
kikoso
  • 673
  • 1
  • 6
  • 17
-1

Please use

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

to bind views (i.e., findViewById).

onCreateView() is used to inflate layout but not to bind views (i.e., findViewById)

satuser
  • 766
  • 8
  • 17