应用程序崩溃(未连接适配器;跳过布局)(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 14675-14675/com.example.bookhub E/RecyclerView: No adapter attached; skipping layout

我已经添加了连接的适配器,但它给出了错误 每次运行应用程序时应用程序都会崩溃

我认为错误出在我声明书信息列表的区域

或者也许在我创建jsonObectRequest的区域

提前致谢

我更深入地研究了您的代码,我认为发生错误是因为您在设置适配器后设置布局管理器,您会尝试将这些行重新组织如下:

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

原因是您的回收器视图在 onCreateView 完成时没有适配器,响应将在创建视图后传递。 所以Recyclerview需要一个没有的适配器。创建一个空适配器,并将其作为适配器传递给onCreateView中的Recyclerview。

为了确保没有数据的适配器不会崩溃,请向其添加以下代码。 在你的DasboardRecyclerAdapter OverloadinggetItemCount像这样

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

請刪除- lateinit var bookInfoList: ArrayListOf

和 add- val bookInfoList= arrayListOf((

问题将得到解决

最新更新