0

Although there are many questions about nested fragments but still it got me stumped in this case.I am making an android app which has an Activity having a frame layout.I am loading a fragment(say Fragment B) into the frame layout.Fragment B has a ViewPager that contains two fragments.First fragment of viewpager(say Fragment VP1) has a gridview that loads images from network.Now on griditem image click I want to show the image in full size in a new fragment which has NetowrkImageView. How do i do this? I tried need to call getChildFragmentManager(),but didn't work. If you guys need code I'll show that. Thanks in advance. onCreateView() of VP1

public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_images_and_videos,null);
        gridItemList=new ArrayList<>();
        gridview= (GridView) view.findViewById(R.id.gridview);
        adapter=new ImagesVideosGridviewAdapter(getActivity(),gridItemList);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // GridItemModel gridItem= (GridItemModel) parent.getItemAtPosition(position);
                Bundle args=new Bundle();
                args.putString("url",gridItemList.get(position).getUrl());
                FragmentTransaction transaction=getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_container_event_specific,new ShowFullImageFragment()).commit();//frame_container_event_specific is present in the main activity.

            }
        });
        new LoadMedia().execute("");
        return view;
    }

Logcat error

java.lang.IllegalArgumentException: No view found for id 0x7f0d008a (com.test.rajat.a10times:id/frame_container_event_specific) for fragment ShowFullImageFragment{ccc6a0 #0 id=0x7f0d008a}
Rajat
  • 99
  • 1
  • 8

1 Answers1

1

Because your VP1 layout doesn't contain R.id.frame_container_event_specific, the fragment manager is unable to find the view. It's in the activity layout, so you better let the fragment manager from the activity to replace the fragment.

But think again about using the fragment approach. I think it's much simpler and easier to make the full image fragment as a standalone Activity. It displays full image anyway. Otherwise, even though you are able to replace the fragment correctly, you have to do a lot to manage the fragment back stack, like replacing the previous fragment when user hits back button.

Aaron He
  • 5,509
  • 3
  • 34
  • 44