【问题标题】:Unable to instantiate fragment com.nisarg.locsav.PhotoGallery: could not find Fragment constructor无法实例化片段 com.nisarg.locsav.PhotoGallery:找不到片段构造函数
【发布时间】:2020-07-20 05:52:40
【问题描述】:

我遇到了一个问题,我创建了一个片段,没有任何问题,但片段仍然没有打开。这是我的片段:

class PhotoGallery extends Fragment {
    RecyclerView recyclerView;
    PhotoGalleryAdapter photoGalleryAdapter;
    PlaceViewmodel placeViewmodel;
    Observer<List<photo_n>> galleryobserver;
    @Override
    public View onCreateView(LayoutInflater infla, ViewGroup parent, Bundle savd)
    {
        View verti=infla.inflate(R.layout.photoviewergallery,parent,false);
        placeViewmodel = new ViewModelProvider(getActivity()).get(PlaceViewmodel.class);
        recyclerView=verti.findViewById(R.id.gridrecycler);
        galleryobserver=new Observer<List<photo_n>>() {
            @Override
            public void onChanged(List<photo_n> photo_ns) {
                ArrayList<photo_n> photo_ns1=new ArrayList<photo_n>();
                for(photo_n photo:photo_ns)
                {
                    photo_ns1.add(photo);
                }
                if(photoGalleryAdapter==null)
                {
                    photoGalleryAdapter=new PhotoGalleryAdapter(getActivity(),photo_ns1);
                    recyclerView.setAdapter(photoGalleryAdapter);
                }
                else
                {
                    photoGalleryAdapter.setPhotoList(photo_ns1);
                }
            }
        };
        placeViewmodel.chosenImages.observe(getViewLifecycleOwner(),galleryobserver);
        if(photoGalleryAdapter==null)
        {
            photoGalleryAdapter=new PhotoGalleryAdapter(getActivity(),new ArrayList<photo_n>());
            recyclerView.setAdapter(photoGalleryAdapter);
        }
        return verti;
    }
}

我什至没有重写构造函数,所以它不应该出现以下问题。但它仍然如此,我已经搜索了堆栈溢出的所有问题但仍然没有得到答案。这里有什么问题?

【问题讨论】:

    标签: android android-fragments android-navigation


    【解决方案1】:

    我已经解决了,你应该正确地写这个:

    class PhotoGallery extends Fragment {
    

    作为

    public class PhotoGallery extends Fragment {
    

    否则它将继续出错。令人惊讶的是,这在我的 android studio 版本中不会自动发生,有人遇到同样的问题吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多