简体   繁体   English

带有 RecyclerView 的片段,没有适配器附加错误

[英]Fragment with RecyclerView, no adapter attached error

I'm making a tabbed activity with different fragments and one of them contains a recyclerview with just some names at the moment.我正在用不同的片段制作一个选项卡式活动,其中一个包含一个 recyclerview,目前只有一些名字。 I have no idea what I've done wrong, I get a warning every time on starting the app that says no adapter attached, skipping layout .我不知道我做错了什么,每次启动应用程序时都会收到一条警告,提示没有附加适配器,跳过布局 I've created and set the adapter to the recyclerview in my fragment, created an adapter with viewholder, and created a class to set the name on the recyclerview.我已经在我的片段中创建了适配器并将其设置为 recyclerview,创建了一个带有 viewholder 的适配器,并创建了一个 class 来设置 recyclerview 上的名称。

Here is my fragment class:这是我的片段 class:

public class fragment_main extends Fragment {

    View view;
    private RecyclerView mainrecyclerview;
    private List<MainSchedule> listStr;

    public fragment_main() {}

    @NonNull
    @Override
    public View onCreateView(LayoutInflater inflater, @NonNull ViewGroup container, @NonNull Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_main, container, false);
        mainrecyclerview = (RecyclerView) view.findViewById(R.id.todayRecyclerView);
        MainRecyclerViewAdapter mainAdapter = new MainRecyclerViewAdapter(getActivity(), listStr);
        mainrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mainrecyclerview.setAdapter(mainAdapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        listStr = new ArrayList<>();
        listStr.add(new MainSchedule("one"));
        listStr.add(new MainSchedule("two"));
        listStr.add(new MainSchedule("three"));

    }
}

fragment.xml:片段.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment_main">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/todayRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="32dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="250dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

recyclerviewadapter class:回收器视图适配器 class:

public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.ViewHolder> {

    Context mContext;
    List<MainSchedule> mData;

    public MainRecyclerViewAdapter(Context context, List<MainSchedule> data){
        this.mContext = context;
        this.mData = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.mainschedule_cards, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.textView.setText(mData.get(position).getName());

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textViewClass);
        }

    }

}

I've tried many ways of creating the adapter and binding the layout manager to the recyclerview, but nothing seems to work and I get the same error each time.我已经尝试了很多方法来创建适配器并将布局管理器绑定到 recyclerview,但似乎没有任何效果,而且我每次都遇到相同的错误。

Problem seems with onCreateView because you're supposed to return view onCreateView似乎有问题,因为您应该返回视图

Replace your fragment_main with code below用下面的代码替换你的fragment_main

public class fragment_main extends Fragment {

    private RecyclerView mainrecyclerview;
    private List<MainSchedule> listStr;

    public fragment_main() {
    }

    @NonNull
    @Override
    public View onCreateView(LayoutInflater inflater, @NonNull ViewGroup container, @NonNull Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listStr = new ArrayList<>();
        listStr.add(new MainSchedule("one"));
        listStr.add(new MainSchedule("two"));
        listStr.add(new MainSchedule("three"));
        mainrecyclerview = (RecyclerView) view.findViewById(R.id.todayRecyclerView);
        MainRecyclerViewAdapter mainAdapter = new MainRecyclerViewAdapter(getActivity(), listStr);
        mainrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mainrecyclerview.setAdapter(mainAdapter);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 片段中的RecyclerView - 没有附加适配器,跳过布局 - RecyclerView in Fragment - No Adapter Attached, Skipping Layout 片段 - E / RecyclerView:没有连接适配器; 跳过布局 - Fragment - E/RecyclerView: No adapter attached; skipping layout 在 Fragment 中使用 RecyclerView 而不是 Activity 会导致 E/RecyclerView: No adapter attached; 跳过布局错误 - Using a RecyclerView inside a Fragment instead of an Activity results in E/RecyclerView: No adapter attached; skipping layout error 没有附加适配器使用 RecyclerView 适配器跳过布局错误 - No Adapter Attached Skipping layout error with RecyclerView Adapter RecyclerView 适配器连接错误:未连接适配器 - RecyclerView adapter connection error: no adapter attached E/RecyclerView 错误:未连接适配器; 跳过布局 - Error with E/RecyclerView: No adapter attached; skipping layout E / RecyclerView:没有连接适配器; 跳过布局错误 - E/RecyclerView: No adapter attached; skipping layout error RecyclerView错误:未连接适配器; 跳过布局 - RecyclerView error: No adapter attached; skipping layout 错误:E / RecyclerView:未连接适配器; 跳过布局 - error: E/RecyclerView: No adapter attached; skipping layout E/RecyclerView:没有附加适配器; 在我的片段中跳过布局 - E/RecyclerView: No adapter attached; skipping layout in my fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM