提问者:小点点

应用程序崩溃(未连接适配器;跳过布局)(Kotlin)


这是我的代码,应用程序一次又一次地崩溃

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
    }


}

它在LOGCAT中给出错误为

06-28 17:07:13.983 1465-14675/com.example.bookhub e/recyclerview:未附加适配器; 跳过布局

我添加了附加的适配器,但它给错误和应用程序崩溃,每次我运行它

我认为错误出现在我声明bookInfoList的区域

或者在我创建jsonObectRequest的区域

提前致谢


共2个答案

匿名用户

我对您的代码进行了更深入的研究,我假设发生错误是因为您在设置适配器之后设置了layoutmanager,您会尝试按以下方式重新组织这些行吗:

recyclerDashboard.layoutManager = layoutManager
recyclerDashboard.addItemDecoration(
                            DividerItemDecoration(
                                recyclerDashboard.context,
                                (layoutManager as LinearLayoutManager).orientation
                            )
                        )
recyclerAdapter = DasboardRecyclerAdapter(activity as Context, bookInfoList)
recyclerDashboard.adapter = recyclerAdapter

匿名用户

原因是当onCreateView完成并且响应将在视图创建后通过时,您的RecycerView没有适配器。 因此Recyclerview需要一个没有这样的适配器。 创建一个空适配器,并将其作为适配器传递给OnCreateView内部的Recyclerview。

为了确保没有数据的适配器不会崩溃,在下面添加代码。 在您的DasboardRecyclerAdapter中重载getItemCount,如下所示

 override fun getItemCount(): Int {
    return bookInfoList.size // your data size
}