当前位置: 首页 > 知识库问答 >
问题:

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

庄新翰
2023-03-14

我一直收到错误“RecycleView:未附加适配器;跳过布局”当显示RecyclView列表时。我有3个选项卡,其中一个选项卡有一个从SQLite数据库填充的RecyclView列表。我没有收到任何崩溃,数据在视图中显示正确,但我仍然收到这个错误。

我以为这只是一个警告,因为数据正确到位,但当我尝试onClick时,它不起作用,我确信它与此错误有关。

我知道这个问题以前被问过很多次,但我检查了大部分问题,没有一个对我有效。

这是我的片段:

public class RecentsFragment extends Fragment {
    DatabaseHelper helper;
    List<MyPojo> dbList;
    RecyclerView mRecyclerView;
    private MyGridAdapter mAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.my_recents_list, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mRecyclerView.setHasFixedSize(true);
        return view;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setHasOptionsMenu(true);
        dbList = new ArrayList<MyPojo>();

        dbList = getCat(); // getCat returns array list from sqlite database

        mAdapter = new MyGridAdapter(dbList);
        mRecyclerView.setAdapter(mAdapter);

    }
}

我的适配器

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

    static List<MyPojo> dbList;
    static Context context;

    MyGridAdapter(Context context, List<MyPojo> dbList){
        this.dbList = new ArrayList<MyPojo>();
        this.context = context;
        this.dbList = dbList;

    }


    MyGridAdapter(List<MyPojo> dbList){
        this.dbList = new ArrayList<MyPojo>();
        this.dbList = dbList;

    }

    @Override
    public MyGridAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.categories_item, null);

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

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

        holder.title.setText(dbList.get(position).getCat());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView title;
        public LinearLayout placeHolder;
        ImageView image;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            title = (TextView) itemLayoutView.findViewById(R.id.placeName);

        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, DetailsActivity.class);

            Bundle extras = new Bundle();
            extras.putInt("catid", dbList.get(getAdapterPosition()).getCatid());
            intent.putExtras(extras);

            context.startActivity(intent);

        }
    }
}

提前感谢

更新:my_recents_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".RecentsFragment">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f8f8f8"
        android:divider="@null"
        android:listSelector="@android:color/transparent"/>

</LinearLayout>

共有1个答案

须原
2023-03-14

为了避免此错误,请避免在CreateView方法中执行findViewById,而是在onViewCreated中执行。

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

    return inflater.inflate(R.layout.my_recents_list, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    mRecyclerView.setHasFixedSize(true);

    setHasOptionsMenu(true);
    dbList = new ArrayList<MyPojo>();

    dbList = getCat(); // getCat returns array list from sqlite database

    mAdapter = new MyGridAdapter(getActivity(), dbList);
    mRecyclerView.setAdapter(mAdapter);

}

编辑

您的适配器应该是这样的:

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

    private List<MyPojo> dbList;
    private Context context;

    MyGridAdapter(Context context, List<MyPojo> dbList) {
        this.dbList = new ArrayList<MyPojo>(dbList);
        this.context = context;
    }

    @Override
    public MyGridAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Note that the inflate method takes the layout to inflate, the parent to measure 
        // the layout and the third parameters is false so only this view will handle events like click event
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.categories_item, parent, false);

        return new ViewHolder(itemLayoutView);
    }

    @Override
    public void onBindViewHolder(final MyGridAdapter.ViewHolder holder, final int position) {
        MyPojo myPojo = dbList.get(position);
        holder.title.setText(myPojo.getCat());
        //You can register the click listener to the textview (or the whole item if you put it into the holder)
        holder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DetailsActivity.class);

            Bundle extras = new Bundle();
            extras.putInt("catid", dbList.get(position).getCatid());
            intent.putExtras(extras);

            context.startActivity(intent);
            }
        });
    }

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

    //The class is static to avoid leaks from a non-static nested class
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView title;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            title = (TextView) itemLayoutView.findViewById(R.id.placeName);

        }
    }
}
 类似资料:
  • 我制作了一个基本的购物清单应用程序,它利用回收器视图来显示列表项目。我正在尝试使用带有片段的导航添加设置屏幕。我遇到了我的回收器视图的问题 主活动.kt HomeFragment.kt 设置Fragment.kt activity_main.xml fragment\u home.xml navigation.xml 不确定是否需要更多信息。提前道歉-我是android studio的新手

  • 但正在记录错误消息: 15: 25:53.476 E/RecyclerView:未连接适配器;跳过布局 15:25:53.655E/RecyClerView:未连接适配器;跳过布局

  • 当应用程序启动时,我得到一个空白屏幕,当我检查日志时,我得到:E/回收人员视图:没有连接适配器;跳过布局错误 我不知道为什么?任何想法,它似乎没有附加回收器视图或添加任何数据,我附加了Main活动、DataAdapter和数据类。 主要活动 数据适配器 下面是我的数据类,将从中提取数据 我ncluded.java 收集器和设置器 这是jsonResponse类,在接口中引用 感谢任何帮助。

  • 图像不显示。在一个SO例子中说要检查适配器中的计数,但我认为它是正确的。我在logcat中得到这个错误 这是代码主活动 这是我的适配器。

  • 给我的 我已经尝试了我找到的所有解决方案(设置一个空适配器,将代码移动到哪里,使用单独的线程),但无济于事。这应该适用于正常的活动,所以我想也许我做错了什么。

  • 我已经创建了一个片段,在该片段中有一个recycle view,但是当我的片段被加载时,什么也没有显示,它给出了这个错误“E/recycle view:没有连接适配器;跳过布局”。下面是适配器和片段类的代码,任何帮助都将不胜感激 适配器类: 片段类: fragment _ view _ all _ my _ recipes . XML view_all_recipe_item.xml