Java RecyclerView适配器连接错误:未连接适配器

Java RecyclerView适配器连接错误:未连接适配器,java,android,android-recyclerview,android-cardview,Java,Android,Android Recyclerview,Android Cardview,我想在片段中使用RecyclerView,但无法连接适配器。当我运行它时,它失败了,我得到了这个错误 E/RecyclerView:未连接适配器;跳过布局 碎片 现在,您正在扩展布局,并返回一个没有为RecyclerView设置适配器的布局。只需返回您已经充气并用于设置RecyclerView的: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,

我想在片段中使用RecyclerView,但无法连接适配器。当我运行它时,它失败了,我得到了这个错误

E/RecyclerView:未连接适配器;跳过布局

碎片


现在,您正在扩展布局,并返回一个没有为RecyclerView设置适配器的布局。只需返回您已经充气并用于设置RecyclerView的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view =inflater.inflate(R.layout.fragment_frag1, container,false);
    rv= (RecyclerView)view.findViewById(R.id.rv);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));

    ArrayList<String> words = new ArrayList<>() ;
    words.add("x");
    words.add("x");
    words.add("x");
    adapter= new CardAdapter(getActivity(),words);
    rv.setAdapter(adapter);

    // Return the view that you already inflated:
    return view;
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u frag1,容器,假);
rv=(RecyclerView)视图.findViewById(R.id.rv);
rv.setHasFixedSize(真);
rv.setLayoutManager(新的LinearLayoutManager(this.getActivity());
ArrayList words=新的ArrayList();
字。添加(“x”);
字。添加(“x”);
字。添加(“x”);
adapter=新卡适配器(getActivity(),words);
rv.设置适配器(适配器);
//返回已充气的视图:
返回视图;
}

现在,您正在膨胀布局,并返回一个没有为RecyclerView设置适配器的布局。只需返回您已经充气并用于设置RecyclerView的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view =inflater.inflate(R.layout.fragment_frag1, container,false);
    rv= (RecyclerView)view.findViewById(R.id.rv);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));

    ArrayList<String> words = new ArrayList<>() ;
    words.add("x");
    words.add("x");
    words.add("x");
    adapter= new CardAdapter(getActivity(),words);
    rv.setAdapter(adapter);

    // Return the view that you already inflated:
    return view;
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u frag1,容器,假);
rv=(RecyclerView)视图.findViewById(R.id.rv);
rv.setHasFixedSize(真);
rv.setLayoutManager(新的LinearLayoutManager(this.getActivity());
ArrayList words=新的ArrayList();
字。添加(“x”);
字。添加(“x”);
字。添加(“x”);
adapter=新卡适配器(getActivity(),words);
rv.设置适配器(适配器);
//返回已充气的视图:
返回视图;
}

您应该返回视图,对布局进行充气并获取recyclerview,不应该充气两次。只需返回第一个充气视图您应该返回视图,对布局进行充气并获取recyclerview,您不应该充气两次。只需返回第一个充气视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view =inflater.inflate(R.layout.fragment_frag1, container,false);
    rv= (RecyclerView)view.findViewById(R.id.rv);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));

    ArrayList<String> words = new ArrayList<>() ;
    words.add("x");
    words.add("x");
    words.add("x");
    adapter= new CardAdapter(getActivity(),words);
    rv.setAdapter(adapter);

    // Return the view that you already inflated:
    return view;
}