ImageAdapter cannot be applied to a Fragment Class

前端 未结 2 1251
长情又很酷
长情又很酷 2021-01-24 02:46

I am trying to follow ImageView and GalleryView tutorial and add a galleryview in a fragment class however there is an error that the ImageAdapter cannot be applied to the Tab1F

相关标签:
2条回答
  • 2021-01-24 02:56

    It needs a Context not a Fragment. Use this new ImageAdapter(getActivity())

    0 讨论(0)
  • 2021-01-24 03:11

    First of all, you should move the return line return inflater.inflate(R.layout.tab2, container, false); to end of your onCreateView, because all other lines will be unreachable.

    Also, you should correct the onCreateView this way :

    View view = inflater.inflate(R.layout.tab2, container, false);
    
        GridView gridview = (GridView) view.findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(getActivity());
    
        .
        .
        .
    
        return view ;
    

    You should pass a context to it. You can use the getActivity(). Also you are missing a closing parenthesis. So, replace the wrong line with

    new ImageAdapter(getActivity()) ;
    
    0 讨论(0)
提交回复
热议问题