我面临 "No adapter attached; skipping layout" 错误

I am facing "No adapter attached; skipping layout" error

当我写我的问题标题时,这个网站会推荐我一些答案,但我找不到我的答案,这就是为什么我要写一个新的。我正在尝试简单的 RecyclerView,它 运行 成功了,但是当我使用 volley 库从互联网上获取数据时,我遇到了这个错误

这是我的代码

BusinessActivity.java

public class BusinessActivity extends AppCompatActivity {

    private final String URL = "https://api.myjson.com/bins/a44ec";
    private JsonArrayRequest request;
    private RequestQueue requestQueue;
    private List<ArticleModel> articleList;
    private RecyclerView recyclerView;

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

        articleList = new ArrayList<>();
        recyclerView = findViewById(R.id.business_recyclerView);
        jsonParse();
    }

    private void jsonParse() {

        request = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                JSONObject jsonObject = null;
                for (int i=0;i<response.length();i++){

                    try {
                        jsonObject = response.getJSONObject(i);
                        ArticleModel article = new ArticleModel();
                        article.setTitle(jsonObject.getString("title"));
                        article.setAuthor(jsonObject.getString("author"));
                        article.setDescription(jsonObject.getString("description"));
                        article.setPublishedAt(jsonObject.getString("publishedAt"));
                        article.setUrlToImage(jsonObject.getString("urlToImage"));
                        articleList.add(article);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                setRecyclerView(articleList);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue = Volley.newRequestQueue(BusinessActivity.this);
        requestQueue.add(request);
    }

    private void setRecyclerView(List<ArticleModel> articleList) {

        BusinessAdapter adapter = new BusinessAdapter(articleList,this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }
}

BusinessAdapter.java

 public class BusinessAdapter extends RecyclerView.Adapter<BusinessAdapter.MyViewHolder> {

    private List<ArticleModel> articleModels;
    private Context context;
    RequestOptions options;

    public BusinessAdapter(List<ArticleModel> articleModels, Context context) {
        this.articleModels = articleModels;
        this.context = context;

        options = new RequestOptions().centerCrop().placeholder(R.drawable.ic_launcher_background).error(R.drawable.ic_launcher_background);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.category_row,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ArticleModel articleModel = articleModels.get(position);

        holder.title.setText(articleModel.getTitle());
        holder.author.setText(articleModel.getAuthor());
        holder.desc.setText(articleModel.getDescription());
        holder.date.setText(articleModel.getPublishedAt());

        Glide.with(context).load(articleModel.getUrlToImage()).apply(options).into(holder.image);
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView title,author,desc,date;
        ImageView image;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            title = itemView.findViewById(R.id.title);
            author = itemView.findViewById(R.id.author);
            desc = itemView.findViewById(R.id.detail);
            date = itemView.findViewById(R.id.date);
            image = itemView.findViewById(R.id.image);
        }
    }
}

错误日志

2020-04-01 00:15:53.117 8821-8821/com.atc.newsappproject E/RecyclerView: No adapter attached; skipping layout
2020-04-01 00:15:53.118 1752-1787/? E/storaged: getDiskStats failed with result NOT_SUPPORTED and size 0

我该如何解决这个问题?

谢谢。

如果您在创建 Fragment 或 Activity 时未设置 RecyclerView,通常会发生此错误。要解决此问题,您需要:

  1. onCreate 中设置您的 RecyclerView,而不是在您获取结果时设置。你用一个空列表设置它。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_business);

    articleList = new ArrayList<>();
    recyclerView = findViewById(R.id.business_recyclerView);
    setRecyclerView(articleList);
    jsonParse();
}
  1. 您在 Adapter
  2. 中创建了一个 setter
public void setArticles(ArrayList<ArticleModel> articles){

    this.articles = articles;
    notifyDataSetChanged();
}
  1. private RecyclerView recyclerView;

  2. 下方创建private BusinessAdapter adapter;
  3. 最后在你的 onResponse 中调用 adapter.setArticles(articlesList);

这里有 link 如果您仍然遇到问题,有很多解决方案: