You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android运行出现RecyclerView: No adapter attached; skipping layout报错求助

问题修复方案

该错误的触发原因是你当前代码仅在allLeadsList非空分支内为RecyclerView设置布局管理器和适配器,若页面初始化阶段列表为空,RecyclerView在首次渲染时没有绑定必要的组件,就会抛出E/RecyclerView: No adapter attached; skipping layout错误,和Android Studio缓存异常无关。

可按以下步骤修复:

  • 初始化逻辑前移:将LayoutManager、Adapter初始化和滚动监听注册逻辑,移到Activity的onCreate或Fragment的onViewCreated等页面初始化生命周期中执行,提前给RecyclerView绑定空数据的适配器占位:
// 页面初始化阶段仅执行一次
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
// 初始化时传入空列表占位
leadsListAdapter = new LeadsListAdapter(getApplicationContext(), adapterOnclick, new ArrayList<>());
recyclerView.setAdapter(leadsListAdapter);

// 滚动监听全局仅注册一次,避免重复触发分页请求
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if (scrollY == v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()) {
            Constants.currentPage++;
            generateNextList(Constants.currentPage);
        }
    }
});
  • 调整数据刷新逻辑:原有数据判断分支只处理视图切换和数据更新,不需要重复初始化RecyclerView相关组件:
if (allLeadsList.isEmpty()) {
    leadsEmptyLayout.setVisibility(View.VISIBLE);
    swipeRefreshLayout.setVisibility(View.GONE);
    searchListLoading.setVisibility(View.GONE);
} else {
    leadsEmptyLayout.setVisibility(View.GONE);
    swipeRefreshLayout.setVisibility(View.VISIBLE);
    // 更新适配器数据
    leadsListAdapter.replaceData(allLeadsList);
    // 加载布局动画
    final LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(LeadHomeActivity.this, R.anim.layout_animation_down_to_up);
    recyclerView.setLayoutAnimation(controller);
    recyclerView.scheduleLayoutAnimation();
}
  • 给适配器新增数据替换方法:在LeadsListAdapter中新增replaceData方法用来更新内部数据集并刷新列表:
// 请将方法中的数据泛型替换为你项目中实际的列表数据类型
public void replaceData(List<LeadsData> newData) {
    this.dataList = newData;
    notifyDataSetChanged();
}

额外注意:部分情况下如果布局文件中RecyclerView的宽高设置为wrap_content也可能触发该警告,建议将宽高设置为match_parent或者固定值,避免布局测量异常。

内容的提问来源于stack exchange,提问作者nerdy kid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 08:15:03