我遇到“未连接适配器;跳过布局”错误

时间:2020-03-31 18:02:13

标签: android android-volley android-recyclerview

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

这是我的代码

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

我该如何解决?

谢谢。

1 个答案:

答案 0 :(得分:1)

当您创建片段或活动时未设置RecyclerView时,通常会发生此错误。要解决此问题,您需要:

  1. RecyclerView中设置onCreate,而不要在拾取结果时。您将其设置为空列表。
@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中创建了一个二传手
public void setArticles(ArrayList<ArticleModel> articles){

    this.articles = articles;
    notifyDataSetChanged();
}
  1. private BusinessAdapter adapter;下创建private RecyclerView recyclerView;

  2. 最后在您的onResponse中,您致电adapter.setArticles(articlesList);

如果您仍然遇到问题,这里有很多解决方案的链接:recyclerview No adapter attached; skipping layout