首页 文章

recyclerview没有连接适配器;跳过布局

提问于
浏览
163

刚刚在我的代码中实现了Recyclerview,取代了Listview .

一切正常 . 显示对象 .

但是logcat说

15:25:53.476 E / RecyclerView:没有连接适配器;跳过布局15:25:53.655 E / RecyclerView:没有连接适配器;跳过布局

代码

ArtistArrayAdapter adapter = new ArtistArrayAdapter(this, artists);
recyclerView = (RecyclerView) findViewById(R.id.cardList);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

正如您所看到的,我为 Recycleview 附加了一个适配器 . 那为什么我一直收到这个错误?

我已经阅读了与同一问题有关的其他问题但没有帮助 .

27 回答

  • 1

    您能否确保从"main"线程调用这些语句(例如在 onCreate() 方法内) . 一旦我从"delayed"方法调用相同的语句 . 在我的情况下 ResultCallback ,我收到相同的消息 .

    在我的 Fragment 中,从 ResultCallback 方法内部调用下面的代码会产生相同的消息 . 在我的应用程序中将代码移动到 onConnected() 方法后,消息消失了......

    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    list.setLayoutManager(llm);
    list.setAdapter( adapter );
    
  • 0

    我收到了相同的两条错误消息,直到我在代码中修复了两件事:

    (1)默认情况下,当您在 RecyclerView.Adapter 中实现方法时,它会生成:

    @Override
    public int getItemCount() {
        return 0;
    }
    

    确保更新代码,使其显示:

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

    显然,如果您的商品中没有商品,那么您将在屏幕上显示零件 .

    (2)我没有这样做,如最佳答案所示:CardView layout_width="match_parent" does not match parent RecyclerView width

    //correct
    LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_listitem, parent, false);
    
    //incorrect (what I had)
    LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_listitem,null);
    

    (3)编辑:奖励:还要确保你像这样设置 RecyclerView

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    

    不喜欢这样:

    <view
        android:id="@+id/RecyclerView"
        class="android.support.v7.widget.RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    我看过一些使用后一种方法的教程 . 虽然它有效但我认为它也会产生这个错误 .

  • 1

    我和你有相同的情况,显示是好的,但错误出现在locat . 这是我的解决方案:(1)初始化RecyclerView&bind适配器ON CREATE()

    RecyclerView mRecycler = (RecyclerView) this.findViewById(R.id.yourid);
    mRecycler.setAdapter(adapter);
    

    (2)获取数据时调用notifyDataStateChanged

    adapter.notifyDataStateChanged();
    

    在recyclerView的源代码中,还有其他线程来检查数据的状态 .

    public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mObserver = new RecyclerView.RecyclerViewDataObserver(null);
        this.mRecycler = new RecyclerView.Recycler();
        this.mUpdateChildViewsRunnable = new Runnable() {
            public void run() {
                if(RecyclerView.this.mFirstLayoutComplete) {
                    if(RecyclerView.this.mDataSetHasChangedAfterLayout) {
                        TraceCompat.beginSection("RV FullInvalidate");
                        RecyclerView.this.dispatchLayout();
                        TraceCompat.endSection();
                    } else if(RecyclerView.this.mAdapterHelper.hasPendingUpdates()) {
                        TraceCompat.beginSection("RV PartialInvalidate");
                        RecyclerView.this.eatRequestLayout();
                        RecyclerView.this.mAdapterHelper.preProcess();
                        if(!RecyclerView.this.mLayoutRequestEaten) {
                            RecyclerView.this.rebindUpdatedViewHolders();
                        }
    
                        RecyclerView.this.resumeRequestLayout(true);
                        TraceCompat.endSection();
                    }
    
                }
            }
        };
    

    在dispatchLayout()中,我们可以发现它中存在错误:

    void dispatchLayout() {
        if(this.mAdapter == null) {
            Log.e("RecyclerView", "No adapter attached; skipping layout");
        } else if(this.mLayout == null) {
            Log.e("RecyclerView", "No layout manager attached; skipping layout");
        } else {
    
  • 0

    我有这个问题,有几次问题是 recycleView put in ScrollView 对象

    检查实施后,原因如下 . 如果将RecyclerView放入ScrollView,那么在测量步骤中,其高度未指定(因为ScrollView允许任何高度),因此,等于最小高度(根据实现),显然为零 .

    您有几种方法可以解决此问题:

    • 将一定高度设置为RecyclerView

    • 将ScrollView.fillViewport设置为true

    • 或者将RecyclerView保留在ScrollView之外 . 在我看来,这是迄今为止最好的选择 . 如果RecyclerView的高度不受限制 - 这是_596582的视图有足够的垂直位置并一次创建的情况 . 没有任何视图再循环,这有点打破了RecyclerView的目的 .

    (也可以跟随 android.support.v4.widget.NestedScrollView

  • 2

    1)创建ViewHolder什么都不做:)

    // SampleHolder.java
    public class SampleHolder extends RecyclerView.ViewHolder {
        public SampleHolder(View itemView) {
            super(itemView);
        }
    }
    

    2)再次创建RecyclerView,什么都不做:)

    // SampleRecycler.java
    public class SampleRecycler extends RecyclerView.Adapter<SampleHolder> {
        @Override
        public SampleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }
    
        @Override
        public void onBindViewHolder(SampleHolder holder, int position) {
    
        }
    
        @Override
        public int getItemCount() {
            return 0;
        }
    }
    

    3)现在当您的真正的回收商还没有准备好时,只需使用下面的样品 .

    RecyclerView myRecycler = (RecyclerView) findViewById(R.id.recycler_id);
    myRecycler.setLayoutManager(new LinearLayoutManager(this));
    myRecycler.setAdapter(new SampleRecycler());
    

    这不是最佳解决方案,但它有效!希望这有用 .

  • 6

    在创建阶段未设置适配器时会发生这种情况:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        ....
    }
    
    public void onResume() {
        super.onResume();
        mRecyclerView.setAdapter(mAdapter);
        ....
    }
    

    只需将适配器设置为带有空数据的onCreate,并在进行数据调用时:

    mAdapter.notifyDataSetChanged();
    
  • 0

    确保通过以下方式设置RecyclerView的布局管理器:

    mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
    

    您也可以使用other布局管理器而不是 LinearLayoutManager .

  • 0

    我有同样的错误,如果你正在等待像我这样的数据使用改造或类似的东西,我修复它

    放在Oncreate之前

    private ArtistArrayAdapter adapter;
    private RecyclerView recyclerView;
    

    把它们放在你的Oncreate中

    recyclerView = (RecyclerView) findViewById(R.id.cardList);
     recyclerView.setHasFixedSize(true);
     recyclerView.setLayoutManager(new LinearLayoutManager(this));
     adapter = new ArtistArrayAdapter( artists , R.layout.list_item ,getApplicationContext());
     recyclerView.setAdapter(adapter);
    

    当你收到数据时

    adapter = new ArtistArrayAdapter( artists , R.layout.list_item ,getApplicationContext());
    recyclerView.setAdapter(adapter);
    

    现在进入您的ArtistArrayAdapter类并执行此操作,如果您的数组为空或为null,它将使GetItemCount返回0,如果不是它将使它成为艺术家数组的大小

    @Override
    public int getItemCount() {
    
        int a ;
    
        if(artists != null && !artists.isEmpty()) {
    
            a = artists.size();
        }
        else {
    
            a = 0;
    
         }
    
       return a;
    }
    
  • 0

    这些行必须在 OnCreate 中:

    mmAdapter = new Adapter(msgList);
    mrecyclerView.setAdapter(mmAdapter);
    
  • 2
    ArtistArrayAdapter adapter = new ArtistArrayAdapter(this, artists);
    recyclerView = (RecyclerView) findViewById(R.id.cardList);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
    

    只需用上面的代码替换它就可以了 . 你做错了是你在调用布局管理器之前调用了setAdapter(adapter) .

  • 1

    这真的是一个简单的错误,你不需要在这里做任何代码 . 由于活动使用的布局文件错误而发生此错误 . 通过IDE,我自动创建了布局的布局v21,该布局成为活动的默认布局 . 我在旧的布局文件中所做的所有代码和新的代码只有很少的xml代码,这导致了这个错误 .

    Solution: Copy all codes of old layout and paste in layout v 21

  • 18

    就我而言,我在适配器里面设置了适配器 onLocationChanged() 回调和调试 . 由于它未检测到位置变化,因此从未触发过 . 当我在模拟器的扩展控件中手动设置它时,它按预期工作 .

  • 179

    我已经解决了这个错误 . 您只需添加布局管理器并添加空适配器 .

    喜欢这段代码:

    myRecyclerView.setLayoutManager(...//your layout manager);
            myRecyclerView.setAdapter(new RecyclerView.Adapter() {
                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    return null;
                }
    
                @Override
                public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    
                }
    
                @Override
                public int getItemCount() {
                    return 0;
                }
            });
    //other code's 
    // and for change you can use if(mrecyclerview.getadapter != speacialadapter){
    //replice your adapter
    //}
    
  • 3

    这个发生的原因是实际的膨胀布局与您在查找recyclerView时引用的布局不同 . 默认情况下,创建片段时,onCreateView方法如下所示: return inflater.inflate(R.layout.<related layout>,container.false);

    而不是那样,单独创建视图并使用它来引用recyclerView View view= inflater.inflate(R.layout.<related layout>,container.false); recyclerview=view.findViewById(R.id.<recyclerView ID>); return view;

  • 9

    首先初始化适配器

    public void initializeComments(){
        comments = new ArrayList<>();
    
        comments_myRecyclerView = (RecyclerView) findViewById(R.id.comments_recycler);
        comments_mLayoutManager = new LinearLayoutManager(myContext);
        comments_myRecyclerView.setLayoutManager(comments_mLayoutManager);
    
        updateComments();
        getCommentsData();
    
    }
    
    public void updateComments(){
    
        comments_mAdapter = new CommentsAdapter(comments, myContext);
        comments_myRecyclerView.setAdapter(comments_mAdapter);
    }
    

    当数据集集发生变化时,只需调用updateComments方法即可 .

  • 0

    检查您是否错过了在适配器中调用此方法

    @Override
    public int getItemCount() {
        return list.size();
    }
    
  • 1

    在我的情况下,它是一个被遗忘的组件,位于ViewHolder类中,但它不在布局文件中

  • 0

    我遇到了同样的问题,并意识到我在从源代码中检索数据后设置了 LayoutManage r和 adapter ,而不是在 onCreate 方法中设置两个 .

    salesAdapter = new SalesAdapter(this,ordersList);
            salesView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            salesView.setAdapter(salesAdapter);
    

    然后通知适配器数据更改

    //get the Orders
                    Orders orders;
                    JSONArray ordersArray = jObj.getJSONArray("orders");
                        for (int i = 0; i < ordersArray.length() ; i++) {
                            JSONObject orderItem = ordersArray.getJSONObject(i);
                            //populate the Order model
    
                            orders = new Orders(
                                    orderItem.getString("order_id"),
                                    orderItem.getString("customer"),
                                    orderItem.getString("date_added"),
                                    orderItem.getString("total"));
                            ordersList.add(i,orders);
                            salesAdapter.notifyDataSetChanged();
                        }
    
  • 0

    此问题是因为您没有为 RecyclerView 添加任何 LayoutManager .

    另一个原因是因为您在NonUIThread中调用此代码 . 确保在UIThread中调用此调用 .

    解决方案只是你必须在UI线程中为 setAdapter 之前为 RecyclerView 添加 LayoutManager .

    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    
  • 1

    我的问题是我的回收站视图看起来像这样

    <android.support.v7.widget.RecyclerView
            android:id="@+id/chatview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent">
        </android.support.v7.widget.RecyclerView>
    

    什么时候应该看起来像这样

    <android.support.v7.widget.RecyclerView
            android:id="@+id/chatview"
            android:layout_width="395dp"
            android:layout_height="525dp"
            android:layout_marginTop="52dp"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="8dp"></android.support.v7.widget.RecyclerView>
    </android.support.constraint.ConstraintLayout>
    
  • 0

    RecyclerView 适配器类中,例如 MyRecyclerViewAdapter ,使用以下参数创建一个构造函数 .

    MyRecyclerViewAdapter(Context context, List<String> data) {
        this.mInflater = LayoutInflater.from(context); // <-- This is the line most people include me miss
        this.mData = data;
    }
    

    mData 是您将传递给适配器的数据 . 如果您没有要传递的数据,则它是可选的 . mInflater 是您创建的 LayoutInflater 对象,您在适配器的 OnCreateViewHolder 函数中使用 .

    在此之后,您可以将适配器附加到MainActivity中,或者在主/ UI线程上正确地将其连接到所需位置

    MyRecyclerViewAdapter recyclerAdapter;
    OurDataStuff mData;
    
        ....
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Like this:
    
            RecyclerView recyclerView = findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerAdapter = new RecyclerAdapter(this, mData); //this, is the context. mData is the data you want to pass if you have any
            recyclerView.setAdapter(recyclerAdapter);
        }
       ....
    
  • 33

    通过在底部设置初始化的空列表和适配器并在获取结果时调用notifyDataSetChanged来解决此问题 .

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerviewItems.setLayoutManager(linearLayoutManager);
        someAdapter = new SomeAdapter(getContext(),feedList);
        recyclerviewItems.setAdapter(someAdapter);
    
  • 0

    我在这个问题上失去了16分钟的生命,所以我只是承认我正在制作的这个令人难以置信的尴尬错误 - 我正在使用Butterknife并在此片段中绑定onCreateView中的视图 .

    花了很长时间才弄清楚为什么我没有布局管理器 - 但显然注入的视图因此它们实际上不会为空,所以回收器永远不会为空...哎呀!

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    
        @Override
    public View onCreateView(......) {
        View v = ...;
        ButterKnife.bind(this, v);
        setUpRecycler()
     }
    
    public void setUpRecycler(Data data)
       if (recyclerView == null) {
     /*very silly because this will never happen*/
           LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
           //more setup
           //...
        }
        recyclerView.setAdapter(new XAdapter(data));
    }
    

    如果您遇到这样的问题,请跟踪您的视图并使用类似 uiautomatorviewer 的内容

  • 1

    对于那些在 fragment 中使用 RecyclerView 并从其他视图中膨胀的人:在膨胀整个片段视图时,请确保将 RecyclerView 绑定到其根视图 .

    我正在连接并正确地为适配器做所有事情,但我从来没有做过绑定 . @Prateek Agarwal的这个answer为我提供了一切,但这里有更多详细说明 .

    Kotlin

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    
        val rootView =  inflater?.inflate(R.layout.fragment_layout, container, false)
        recyclerView = rootView?.findViewById(R.id.recycler_view_id)
        // rest of my stuff
        recyclerView?.setHasFixedSize(true)
        recyclerView?.layoutManager = viewManager
        recyclerView?.adapter = viewAdapter
        // return the root view
        return rootView
    }
    

    Java

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        View rootView= inflater.inflate(R.layout.fragment_layout,container.false);
        recyclerview= rootView.findViewById(R.id.recycler_view_id);
        // rest ...
        return rootView;
    }
    
  • 8

    答案不是很多,但即使代码正确,我也遇到了同样的问题 . 有时,它只是最简单的制作 . 在这种情况下,OP的错误也可能从Manifest中丢失 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ,它会再现相同的错误 .

  • 0

    在我的情况下,它发生了因为我在LinearLayout中嵌入了RecyclerView .

    我以前有一个只包含一个根RecyclerView的布局文件,如下所示

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/fragment_products"
    
        android:name="Products.ProductsFragment"
        app:layoutManager="LinearLayoutManager"
        tools:context=".Products.ProductsFragment"
    
        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"/>
    

    我相信这个问题是在3行内分开的 . 无论如何,我认为这是一个简单的问题,明天就要开始工作;以为我应该在忘记这个帖子之前写下我发现的东西 .

  • 1

    只需将以下内容添加到 RecyclerView

    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    

    例:

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
        </android.support.v7.widget.RecyclerView>
    

相关问题