-1

I've done everything right but my logcat still shows this error No adapter attached; skipping layout

I have tried setting an empty adapter first inside of onCreate() and adapter.notifyDataSetChanged(); after I get the data in my praseJSON() but still no luck!

Everything in the Recycler adapter file is also set according to all the fix is found online.


private static final String TAG = "Nearby";
    private RecyclerView mRecyclerView;
    private NearbyAdapter mNearbyAdapter;
    private ArrayList<NearbyItem> mNearbyList;
    private RequestQueue mRequestQueue;
    private LocationManager locationManager;
    private String apiURL = "";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nearby);
        mRecyclerView = findViewById(R.id.recyclerViewNearby);
        mRecyclerView.setAdapter(mNearbyAdapter);
        mNearbyAdapter = new NearbyAdapter(NearbyActivity.this, mNearbyList);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mNearbyList = new ArrayList<>();
        mRequestQueue = Volley.newRequestQueue(this);
        parseJSON();

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED)
        {
            return;
        }

        Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
        onLocationChanged(location);
    }


private void parseJSON()
    {
        Log.d(TAG, "Starting parseJSON()"+TAG);
        String url = apiURL;
        Log.d(TAG,"URL: " + url);

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject response)
                    {
                        try
                        {
                            JSONArray jsonArray = response.getJSONArray("member");
                            for (int i = 0; i < jsonArray.length(); i++)
                            {
                                JSONObject member = jsonArray.getJSONObject(i);
                                String nName = "";
                                String nType = "";
                                String nDesc = "";
                                if (member.has("name")){
                                    nName = member.getString("name");
                                    Log.d(TAG, "Name is: " + nName);
                                }
                                if(member.has("type")) {
                                    nType = member.getString("type");
                                    Log.d(TAG, "Type is: " + nType);
                                }
                                if(member.has("description")){
                                    nDesc = member.getString("description");
                                    Log.d(TAG, "Desc is: " + nDesc);
                                }
                                else
                                {
                                    nDesc += nName;
                                    Log.d(TAG, "Desc is: " + nDesc);
                                }

                                if(nType.contains("bus_stop")){
                                    nType = "Bus Stop";

                                }
                                else if (nType.contains("train_station"))
                                {
                                    nType = "Train Station";
                                }
                                mNearbyList.add(new NearbyItem(nName, nType, nDesc));
                            }
                            mNearbyAdapter.notifyDataSetChanged();
                            mNearbyAdapter = new NearbyAdapter(NearbyActivity.this, mNearbyList);
                            mRecyclerView.setAdapter(mNearbyAdapter);

                        }
                        catch (JSONException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                error.printStackTrace();
            }
        });
        mRequestQueue.add(request);
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Vish
  • 3
  • 4

1 Answers1

1

The problem is that you've set the adapter mNearbyAdapter before creating the NearbyAdapter object.

protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nearby);
        mRecyclerView = findViewById(R.id.recyclerViewNearby);

        // you passed mNearbyAdapter here
        mRecyclerView.setAdapter(mNearbyAdapter);  

        // but you created the object for it here
        mNearbyAdapter = new NearbyAdapter(NearbyActivity.this, mNearbyList);
        ...

To fix this, create the object for your adapter first, then call setAdapter(mNearbyAdapter).

user3170251
  • 310
  • 1
  • 13