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

equ*_*arn 213 android recycler-adapter android-recyclerview

刚刚在我的代码中实现了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));
Run Code Online (Sandbox Code Playgroud)

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

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

Pet*_*ter 223

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

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

LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(llm);
list.setAdapter( adapter );
Run Code Online (Sandbox Code Playgroud)

  • 只需先设置一个空的适配器,一旦有数据就更新它(这对我有用). (85认同)
  • @equitharn你应该首先设置一个空的适配器,然后下载数据,调用mAdapter.notifyDataSetChanged(),这对我有用. (10认同)
  • 但我需要先下载数据然后再显示! (6认同)
  • 这就是yigit建议的! (3认同)
  • 此解决方案无济于事。实际上,该线程中没有解决方案对我有所帮助。有人可以在这里查看我的情况吗?(/sf/ask/3095766271/) (2认同)

Mic*_*cro 40

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

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

@Override
public int getItemCount() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

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

@Override
public int getItemCount() {
    return artists.size();
}
Run Code Online (Sandbox Code Playgroud)

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

(2)我没有这样做,如上面的答案所示:CardView layout_width ="match_parent"与父级RecyclerView宽度不匹配

//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);
Run Code Online (Sandbox Code Playgroud)

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/RecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
Run Code Online (Sandbox Code Playgroud)

不喜欢这样:

<view
    android:id="@+id/RecyclerView"
    class="android.support.v7.widget.RecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

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

  • 天啊.那一刻,当你回到SO,并看到你已经投票的答案,并意识到你做了同样愚蠢的事情.再次感谢@Micro. (4认同)

小智 19

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

RecyclerView mRecycler = (RecyclerView) this.findViewById(R.id.yourid);
mRecycler.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

(2)获取数据时调用notifyDataStateChanged

adapter.notifyDataStateChanged();
Run Code Online (Sandbox Code Playgroud)

在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();
                }

            }
        }
    };
Run Code Online (Sandbox Code Playgroud)

在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 {
Run Code Online (Sandbox Code Playgroud)


小智 11

我有这个问题,一些时间问题是放在ScrollView对象中的recycleView

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

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

  1. 将一定高度设置为RecyclerView
  2. 将ScrollView.fillViewport设置为true
  3. 或者将RecyclerView保留在ScrollView之外.在我看来,这是迄今为止最好的选择.如果RecyclerView的高度不受限制 - 这是放入ScrollView时的情况 - 那么所有Adapter的视图都有足够的垂直位置并一次创建.没有任何视图再循环,这有点打破了RecyclerView的目的.

(也可以跟着android.support.v4.widget.NestedScrollView)


Mad*_*ota 10

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

// SampleHolder.java
public class SampleHolder extends RecyclerView.ViewHolder {
    public SampleHolder(View itemView) {
        super(itemView);
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

RecyclerView myRecycler = (RecyclerView) findViewById(R.id.recycler_id);
myRecycler.setLayoutManager(new LinearLayoutManager(this));
myRecycler.setAdapter(new SampleRecycler());
Run Code Online (Sandbox Code Playgroud)

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


Pet*_*ile 8

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

@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);
    ....
}
Run Code Online (Sandbox Code Playgroud)

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

mAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)


Rav*_*yal 6

在 Kotlin 中,我们遇到了这个奇怪的不合逻辑的问题。

这不起作用:

 mBinding.serviceProviderCertificates.apply {
            adapter = adapter
            layoutManager =  LinearLayoutManager(activity)
        }
Run Code Online (Sandbox Code Playgroud)

虽然这有效:

mBinding.serviceProviderCertificates.adapter = adapter
mBinding.serviceProviderCertificates.layoutManager = LinearLayoutManager(activity)
Run Code Online (Sandbox Code Playgroud)

一旦我在下班后得到更多的信息,我就会分享更多的见解。


Sum*_*Jha 5

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

mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
Run Code Online (Sandbox Code Playgroud)

除了LinearLayoutManager,您也可以使用其他布局管理器。


小智 5

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

@Override
public int getItemCount() {
    return list.size();
}
Run Code Online (Sandbox Code Playgroud)