This is my code and the app is crashing again and again
package com.example.bookhub.adapter.fragment
import android.app.AlertDialog
import android.app.DownloadManager
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.textclassifier.TextClassification
import android.widget.Button
import android.widget.Toast
import androidx.recyclerview.widget.DividerItemDecoration
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.bookhub.R
import com.example.bookhub.adapter.adapter.DasboardRecyclerAdapter
import com.example.bookhub.adapter.model.Book
import com.example.bookhub.adapter.util.ConnectionManager
class dashboardFragment : Fragment() {
lateinit var recyclerDashboard: RecyclerView
lateinit var layoutManager: RecyclerView.LayoutManager
lateinit var btnCheckInternet: Button
lateinit var recyclerAdapter: DasboardRecyclerAdapter
lateinit var bookInfoList: ArrayList<Book>
/* var bookInfoList = arrayListOf<Book>(
Book("P.S. I love You", "Cecelia Ahern", "Rs. 299", "4.5", 77777,777),
Book("The Great Gatsby", "F. Scott Fitzgerald", "Rs. 399", "4.1", R.drawable.great_gatsby),
Book("Anna Karenina", "Leo Tolstoy", "Rs. 199", "4.3", R.drawable.anna_kare),
Book("Madame Bovary", "Gustave Flaubert", "Rs. 500", "4.0", R.drawable.madame),
Book("War and Peace", "Leo Tolstoy", "Rs. 249", "4.8", R.drawable.war_and_peace),
Book("Lolita", "Vladimir Nabokov", "Rs. 349", "3.9", R.drawable.lolita),
Book("Middlemarch", "George Eliot", "Rs. 599", "4.2", R.drawable.middlemarch),
Book("The Adventures of Huckleberry Finn", "Mark Twain", "Rs. 699", "4.5", R.drawable.adventures_finn),
Book("Moby-Dick", "Herman Melville", "Rs. 499", "4.5", R.drawable.moby_dick),
Book("The Lord of the Rings", "J.R.R Tolkien", "Rs. 749", "5.0", R.drawable.lord_of_rings)
)*/
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this com.example.bookhub.com.example.bookhub.adapter.adapter.fragment
val view = inflater.inflate(R.layout.fragment_dashboard, container, false)
recyclerDashboard = view.findViewById(R.id.recylerDashboard)
btnCheckInternet = view.findViewById(R.id.btnCheckInternet)
btnCheckInternet.setOnClickListener {
if (ConnectionManager().checkConnectivity(activity as Context)) {
//internet is available
val dialog = AlertDialog.Builder(activity as Context)
dialog.setTitle("Success")
dialog.setMessage("Internet Connection Found")
dialog.setPositiveButton("Ok") { text, listner ->
}
dialog.setNegativeButton("Cancel") { text, listner ->
}
dialog.create()
dialog.show()
} else {
//internet not available
val dialog = AlertDialog.Builder(activity as Context)
dialog.setTitle("Error")
dialog.setMessage("Internet Connection Not Found")
dialog.setPositiveButton("Ok") { text, listner ->
}
dialog.setNegativeButton("Cancel") { text, listner ->
}
dialog.create()
dialog.show()
}
}
layoutManager = LinearLayoutManager(activity)
val queue = Volley.newRequestQueue(activity as Context)
val url = "http://13.235.250.119/v1/book/fetch_books/"
val jsonObjectRequest =
object : JsonObjectRequest(Request.Method.GET, url, null, Response.Listener {
// Here we will handle the respose
val success = it.getBoolean("success")
if (success) {
val data = it.getJSONArray("data")
for (i in 0 until data.length()) {
val bookJsonObject = data.getJSONObject(i)
val bookObject = Book(
bookJsonObject.getString("book_id"),
bookJsonObject.getString("name"),
bookJsonObject.getString("author"),
bookJsonObject.getString("rating"),
bookJsonObject.getString("price"),
bookJsonObject.getString("image")
)
bookInfoList.add(bookObject)
recyclerAdapter = DasboardRecyclerAdapter(activity as Context, bookInfoList)
recyclerDashboard.adapter = recyclerAdapter
recyclerDashboard.layoutManager = layoutManager
recyclerDashboard.addItemDecoration(
DividerItemDecoration(
recyclerDashboard.context,
(layoutManager as LinearLayoutManager).orientation
)
)
}
} else {
Toast.makeText(activity as Context, "Some ERROR OCCURED", Toast.LENGTH_SHORT).show()
}
}, Response.ErrorListener {
// here we handel the error
println("Error is $it")
}) {
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["Content-type"] = "application/json"
headers["token"] = "fe******8a1ec7"
return headers
}
}
queue.add(jsonObjectRequest)
return view
}
}
It gives error in LOGCAT as
06-28 17:07:13.983 14675-14675/com.example.bookhub E/RecyclerView: No adapter attached; skipping layout
I have added attached the adapter yet it is giving error and the app crashes every time I run it
I think the error is in the area where I declared bookInfoList
or Maybe in the area where I created jsonObectRequest
Thanks in Advance