-1

Getting an error in logcat window of 2020-07-03 12:00:51.501 5424-5424/com.example.foodify E/RecyclerView: No adapter attached; skipping layout. App is not crashing but is not fetching data from server .I checked for every possible errors. URL and token are correct, net is also working properly. Even getting a response from server when used println() statement. Even I have attached adapter properly as shown in code below:

package com.example.foodify.fragment

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.example.foodify.R
import com.example.foodify.adapter.HomeRecyclerAdapter
import com.example.foodify.model.Restaurant
import com.example.foodify.util.ConnectionManager
import org.json.JSONException


class HomeFragment : Fragment() {

lateinit var recyclerHome : RecyclerView
lateinit var layoutManager: RecyclerView.LayoutManager
lateinit var recyclerAdapter: HomeRecyclerAdapter
lateinit var progressLayout: RelativeLayout
lateinit var progressBar: ProgressBar

val restaurantList= arrayListOf<Restaurant>()

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view= inflater.inflate(R.layout.fragment_home, container, false)

    recyclerHome = view.findViewById(R.id.recyclerHome)
    layoutManager = LinearLayoutManager(activity)
    progressBar= view.findViewById(R.id.progressBar)
    progressLayout = view.findViewById(R.id.progressLayout)
    progressLayout.visibility = View.VISIBLE

    val queue = Volley.newRequestQueue(activity as Context)
    val url = "http://13.235.250.119/v2/restaurants/fetch_result/"

    if(ConnectionManager().checkConnectivity(activity as Context)){

        val jsonObjectRequest = object: JsonObjectRequest(Request.Method.GET, url, null, Response.Listener {

            try {
                progressLayout.visibility = View.GONE
                val success = it.getBoolean("success")

                if(success){
                    val data = it.getJSONArray("data")
                    for(i in 0 until data.length()){
                        val restaurantJsonObject = data.getJSONObject(i)
                        val restaurantObject = Restaurant(
                            restaurantJsonObject.getString("id"),
                            restaurantJsonObject.getString("name"),
                            restaurantJsonObject.getString("rating"),
                            restaurantJsonObject.getString("cost_for_one"),
                            restaurantJsonObject.getString("image_url")
                        )

                        restaurantList.add(restaurantObject)
                        recyclerAdapter = HomeRecyclerAdapter(activity as Context, restaurantList)
                        recyclerHome.adapter = recyclerAdapter
                        recyclerHome.layoutManager = layoutManager
                    }
                }
                else{
                    Toast.makeText(activity as Context, "Some Error Occurred !!", Toast.LENGTH_SHORT).show()
                }
            }
            catch (e: JSONException){
                Toast.makeText(activity as Context, "Some JSON Exception Occurred !!", Toast.LENGTH_SHORT).show()
            }

        },Response.ErrorListener {

            if(activity!=null){
                Toast.makeText(activity as Context, "Volley error occurred !!", Toast.LENGTH_SHORT).show()
            }

        }){
            override fun getHeaders(): MutableMap<String, String> {
                val headers = HashMap<String, String>()
                headers["Content-type"]="application/json"
                headers["token"]="9786fdf7780ee4"
                return headers
            }
        }
        queue.add(jsonObjectRequest)
    }
    else{
        //Internet is not available
        val dialog = AlertDialog.Builder(activity as Context)
        dialog.setTitle("Error")
        dialog.setMessage(" No Internet Connection Found")
        dialog.setPositiveButton("Open Settings"){ text, listener ->
            val settingsIntent = Intent(Settings.ACTION_WIRELESS_SETTINGS)
            startActivity(settingsIntent)
            activity?.finish()
        }

        dialog.setNegativeButton("Exit"){ text, listener ->
            ActivityCompat.finishAffinity(activity as Activity)
        }
        dialog.create()
        dialog.show()
    }


    return view

}


}
  • 1
    Does this answer your question? [recyclerview No adapter attached; skipping layout](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – AgentP Jul 03 '20 at 07:17
  • This is the answer that is use, https://stackoverflow.com/a/30581896/4160896 . I forgot adding 'linearManager.setOrientation(LinearLayoutManager.VERTICAL);' . After adding this, the error is gone. Good luck. – QuartZ Mar 06 '21 at 07:13

2 Answers2

0

You should attach layout manager and adapter before returning view:

 recyclerHome = view.findViewById(R.id.recyclerHome)
 layoutManager = LinearLayoutManager(activity)
 recyclerAdapter = HomeRecyclerAdapter(activity as Context, emptyList())
 recyclerHome.adapter = recyclerAdapter
 recyclerHome.layoutManager = layoutManager

And after getting server response, update content in adapter and invoke notifyDataSetChanged()

S-Sh
  • 3,564
  • 3
  • 15
  • 19
0

Please setLayoutManager with your recyclerview before setting an adapter.

Follow the Sequence of statements:

recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
adapter = new MyAdapter();
recyclerView.setAdapter(adapter);
Apps Maven
  • 1,314
  • 1
  • 4
  • 17