1

When I am writing the title of my question, then this site recommends me some answer but I can't find my answer that's why I am writing a new one. I am trying simple RecyclerView and it's run successfully but when I am fetching data from internet using volley library then I face this error

here is my code

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

Error log

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

How can I solve this?

Thank you.

Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
user10997009
  • 142
  • 8

1 Answers1

1

This error usually happens when you don't set the RecyclerView when you create your Fragment or Activity. To fix this you need to:

  1. Set your RecyclerView in onCreate instead of when you pick up result. You set it with an empty list.
@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. You create a setter in your Adapter
public void setArticles(ArrayList<ArticleModel> articles){

    this.articles = articles;
    notifyDataSetChanged();
}
  1. Create private BusinessAdapter adapter; below private RecyclerView recyclerView;

  2. Finally in your onResponse you call adapter.setArticles(articlesList);

Here is a link with a lot of solutions if you still got issue: recyclerview No adapter attached; skipping layout

Biscuit
  • 4,840
  • 4
  • 26
  • 54
  • Thank u for your response,but i can't understand this line Finally in your onResponse you call recyclerView.setArticles(articlesList),can u explain.@Biscuit – user10997009 Apr 01 '20 at 08:19
  • 1
    Instead of using `setRecyclerView(articleList);` after your `try/catch` you use `recyclerView.setArticles(articlesList)` – Biscuit Apr 01 '20 at 08:30
  • but i can't access the setter recyclerView.setArticles(articlesList) @Biscuits – user10997009 Apr 01 '20 at 08:51
  • i tried your code but it's show nothing. here is the updated code in your suggestion https://gist.github.com/diptoroy/c8e1e4cb11fcc4414dc7fc995c878917 @Biscuit – user10997009 Apr 01 '20 at 09:04
  • please check the link,Thank u – user10997009 Apr 01 '20 at 09:08
  • You have to modify your `setRecyclerView` with the new variable that you created. `businessAdapter = new BusinessAdapter(articleList,this);` instead of `BusinessAdapter adapter = new BusinessAdapter(articleList,this);` – Biscuit Apr 01 '20 at 09:09
  • I know i am annoying but can u explain more.Sorry @Biscuit – user10997009 Apr 01 '20 at 09:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210723/discussion-between-biscuit-and-user10997009). – Biscuit Apr 01 '20 at 09:13