Image GridView Inside Fragment

ぃ、小莉子 提交于 2019-11-30 09:34:47

It turns out a few simple modifications to the original code and it works.

After debugging and setting breakpoints I was able to find that the context in the PhotoImageAdapter was a null pointer and therefore, causing the app to crash. It was the way I was initialising the adapter in my PhotoFragment and also the method I was initialising it in. Below is the code that works correctly for anyone else who is struggling with this.

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.photos_layout,container,false);
        GridView gridView = (GridView) view.findViewById(R.id.photogridview);
        gridView.setAdapter(new MyAdapter(view.getContext())); // uses the view to get the context instead of getActivity().
        return view;
    }

Again this might not be the best or the only of doing it but this way worked for me. (PhotoImageAdapter.java had a name change to MyAdapter.java)

hi chek the code which i just shared, i made the fragment with gridview so hopefully it will helpful for you check this link, there you will find full detail about gridview implementation with fragment for both device and tablet all the best

check this files mainly in my answer it will sure helpful for u MasterFragment,MasterGridActivity,MyAdapter,gridview.xml

How to Show Different Layouts inside Fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!