0

I keep getting this message in Logcat: E/RecyclerView: No adapter attached; skipping layout

I searched quite some time so far on StackOverflow but answers don't seem to work for me. It looks to me that the adapter is set correctly. I'm trying to implement a messenger-functinality to an app in Android.

I'll post the code below.

public class MessageListActivity extends AppCompatActivity {
    public RecyclerView mMessageRecycler;
    public MessageListAdapter mMessageAdapter;
    ArrayList<String> msgidlist = new ArrayList<String>();
    RequestQueue rq;
    String url, id_from, id_to, msgid, m_from, m_to, mnicksender, mnickreceiver, mbody,mtimestamp, mwasread;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_list);
        id_to="2243";
        id_from="5073";
        url = "https://www.someurl.com/chat.php?id1="+id_from+"&id2="+id_to;
        rq = Volley.newRequestQueue(this);
        sendjsonrequest();
    }

    public void sendjsonrequest() {
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                // Process the JSON
                try{
                    // Loop through the array elements
                    for(int i=0;i<response.length();i++){
                        // Get current json object
                        JSONObject messages = response.getJSONObject(i);
                        // Get the current profile (json object) data
                        msgid = messages.getString("msgid");
                        m_from = messages.getString("id_from");
                        m_to = messages.getString("id_to");
                        mnicksender = messages.getString("sender");
                        mnickreceiver = messages.getString("receiver");
                        mbody = messages.getString("body");
                        mtimestamp = messages.getString("timestamp");
                        mwasread = messages.getString("wasread");
                        messageData.setmsgid(msgid);
                        messageData.setm_from(m_from);
                        messageData.setm_to(m_to);
                        messageData.setmnicksender(mnicksender);
                        messageData.setMnickreceiver(mnickreceiver);
                        messageData.setBody(mbody);
                        messageData.setTimestamp(mtimestamp);
                        messageData.setWasread(mwasread);
                        msgidlist.add(msgid);
                    }
                }catch (JSONException e){
                    e.printStackTrace();
                }
                mMessageRecycler = findViewById(R.id.reyclerview_message_list);
                mMessageAdapter = new MessageListAdapter(MessageListActivity.this, msgidlist);
                mMessageRecycler.setLayoutManager(new LinearLayoutManager(MessageListActivity.this));
                mMessageRecycler.setAdapter(mMessageAdapter);
            }

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

}

Any ideas on how to deal with this.

Fredds
  • 151
  • 2
  • 10
  • Does it actually not work? You'll see that error because you're not immediately attaching an adapter. But once you do set it, it should fill in data. You should probably be checking whether or not `msgidlist` is empty. Check the `error` passed in `onErrorResponse()`; there could have been a connection problem. – TheWanderer Nov 04 '18 at 17:09

2 Answers2

1

Your code is fine, but need some small changes

Add the below line of code in OnCreate. Remember, always initialize your UI widgets in OnCreate or in OnResume state of your Activity/Fragment.

mMessageRecycler = findViewById(R.id.reyclerview_message_list);

Then Add this line inside OnCreate or OnResume after the declaration of Recyclerview widget and this line also.

mMessageRecycler.setHasFixedSize(true);

mMessageRecycler.setLayoutManager(new LinearLayoutManager(MessageListActivity.this));

Then after completing the above lines, you can keep your Adapter initialization inside of your API response call.

hemandroid
  • 568
  • 4
  • 12
  • It does give some error on the sethasfixedsize(true). Any reasons why you would implement this? – Fredds Nov 04 '18 at 17:43
  • can you show me the logcat message, then I can tell you the exact solution for your problem? Working with recyclerview is not at all a big deal – hemandroid Nov 04 '18 at 19:32
1

Add following two lines in onCreate() method and remove from the onResponse(). In this case it will generate nullpointer error or skipping layout because of view not find or attached with RecyclerView object. So always assign your view in onCreate() or onResume() method of activity.

 mMessageRecycler = findViewById(R.id.reyclerview_message_list);
 mMessageRecycler.setLayoutManager(new LinearLayoutManager(MessageListActivity.this));
Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
  • @Fredds try this – Saurabh Bhandari Nov 04 '18 at 17:27
  • 1
    This looks like solved the issue. Now I can dive in the newly popped up issue with my adapter :). Thanks – Fredds Nov 04 '18 at 17:42
  • There is an issue left. When doing this I get this error: (java nullpointerexception) caused by: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference at com.smartvibes.smartbeat.MessageListActivity.onCreate(MessageListActivity.java:40) – Fredds Nov 04 '18 at 18:03
  • it will occur when you try to set layout manager before declaring recyclerView because at that time recyclerView is null. so first declare it by findviewById and then add your layout manager – Saurabh Bhandari Nov 04 '18 at 18:13
  • Bizarre. Now I have the no adapter attached, skipping layout error again. protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_message_list); mMessageRecycler = findViewById(R.id.reyclerview_message_list); mMessageRecycler.setLayoutManager(new LinearLayoutManager(MessageListActivity.this)); -further down- mMessageAdapter = new MessageListAdapter(MessageListActivity.this, msgidlist); mMessageRecycler.setAdapter(mMessageAdapter); } – Fredds Nov 04 '18 at 18:35
  • @Fredds yes it will give you this warning in case of onErrorResonse method called. because you have set your adapter in onResonse method. – Saurabh Bhandari Nov 05 '18 at 01:56