刚刚在我的代码中实现了RecyclerView,取代了ListView。

一切都很好。显示数据。

但是错误消息正在被记录:

15:25:53.476 E/RecyclerView: No adapter attached; skipping layout

15:25:53.655 E/RecyclerView: No adapter attached; skipping layout

对于以下代码:

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

正如您所看到的,我已经为RecyclerView附加了一个适配器。 为什么我总是得到这个错误呢?

我读过与这个问题相关的其他问题,但没有一个有用。


当前回答

你能确保你在延迟异步回调(例如在onCreate()方法内部)之外的“主线程”调用这些语句吗? 只要我从“延迟”方法调用相同的语句。在我的例子中是ResultCallback,我得到了相同的消息。

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

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

其他回答

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

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

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

默认情况下,当你在RecyclerView中实现方法时。它生成的适配器:

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

确保你更新了你的代码,让它说:

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

显然,如果你的项目中没有项目,那么屏幕上显示的东西就会是零。

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

(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" />

我看过一些使用后一种方法的教程。当它工作时,我认为它也会产生这个错误。

//当你在创建阶段没有设置适配器时发生:当api response得到Its Working时调用notifyDataSetChanged()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);


        magazineAdapter = new MagazineAdapter(getContext(), null, this );
        newClipRecyclerView.setAdapter(magazineAdapter);
        magazineAdapter.notifyDataSetChanged();

       APICall();
}

public void APICall() {
    if(Response.isSuccessfull()){
    mRecyclerView.setAdapter(mAdapter);
   }
}
Just move setting the adapter into onCreate with an empty data and when you have the data call:

mAdapter.notifyDataSetChanged();

当你在创建阶段没有设置适配器时,会发生这种情况:

@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();

添加另一个答案,因为我遇到了这个线程谷歌错误。我试图初始化一个PreferenceFragmentCompat,但我忘记在onCreatePreferences中像这样膨胀首选项XML:

class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val inflater = LayoutInflater.from(context)
        inflater.inflate(R.layout.fragment_settings, null)
    }

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        // Missing this line here:
        //   setPreferencesFromResource(R.xml.settings, rootKey)
    }
}

这个错误一直是个谜,直到我意识到PreferenceFragmentCompat必须在内部使用RecyclerView。