Image GridView Inside Fragment

前端 未结 2 901
长发绾君心
长发绾君心 2020-12-31 17:51

I have just started to develop on the android platform after developing on iOS. I have looked around and I can\'t seem to figure it out. I am trying to have a grid view appe

2条回答
  •  死守一世寂寞
    2020-12-31 18:25

    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)

提交回复
热议问题