當我運行我的 android 應用程式時,我的應用程式崩潰了。誰能找出我的錯誤?
這是我的error:
kotlin.UninitializedPropertyAccessException: lateinit property layoutManager has not been initialized
at com.example.bookfactory.DashboardFragment.getLayoutManager(DashboardFragment.kt:27)
at com.example.bookfactory.DashboardFragment.onCreateView$lambda-5(DashboardFragment.kt:149)
at com.example.bookfactory.DashboardFragment.$r8$lambda$kfcXUUMc72SMC9NHSb3PcIuvj10(Unknown Source:0)
at com.example.bookfactory.DashboardFragment$$ExternalSyntheticLambda6.onResponse(Unknown Source:4)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:100)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
這里是 DashboardFragment.kt
package com.example.bookfactory
import android.app.AlertDialog
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.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.bookfactory.model.Book
import com.example.bookfactory.util.ConnectionManager
class DashboardFragment : Fragment() {
lateinit var recyclerdashboard:RecyclerView
lateinit var layoutManager: RecyclerView.LayoutManager
lateinit var btncheckinternet:Button
/*val booklist= arrayListOf(
"wings of fire",
"rich dad poor dad",
"The empathy edge",
"believe in yourself",
"think and grow rich",
"the screct",
"the subtle art of not giving a fuck",
"the power of positive thinking",
"nelson mandela long walk to freedom",
"Bhagavad-Gita"
)*/
lateinit var recycleradapter: DashboardRecyclerAdapter
var bookinfolist= arrayListOf<Book>()
/* val bookinfolist= arrayListOf<Book>(
Book("Wings of fire","APJ abdul kalam","Rs. 179","4.5",R.drawable.wingoffire123),
Book("Rich dad poor dad","Robert Kiyosaki","Rs. 199","4.5",R.drawable.richdadpoordad),
Book("The empathy edge","Maria Ross","Rs. 159","3.6",R.drawable.empathyedge),
Book("Believe in yourself","Dr. Murphy Joseph","Rs. 219","4.0",R.drawable.believeinurself),
Book("Think and grow rich","Napoleon Hill","Rs. 279","4.2",R.drawable.thinkandgrowrich),
Book("The secret","Rhonda Byrne","Rs. 299","5.0",R.drawable.thesecret),
Book("The subtle art of not giving a fuck","Mark Manson","Rs. 399","4.5",R.drawable.thesubtitleartof),
Book("The power of positive thinking","Norman Vincent Peale","Rs. 249","4.1",R.drawable.powerofpositive),
Book("Nelson mandela long walk to freedom","Nelson mandela","Rs. 349","4.6",R.drawable.longwalktofreedom),
Book("Bhagavad-Gita","Vyasudu","249","5.0",R.drawable.bhagavadgita)
)*/
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view=inflater.inflate(R.layout.fragment_dashboard, container, false)
btncheckinternet= view.findViewById(R.id.btncheckinternet)
btncheckinternet.setOnClickListener{
if(ConnectionManager().checkConnectivity(activity as Context)){
val dialog=AlertDialog.Builder(activity as Context)
dialog.setTitle("success")
dialog.setMessage("Internet Connection Found")
dialog.setPositiveButton("ok"){text,listener->
}
dialog.setNegativeButton("Cancel"){text,listener->
}
dialog.create()
dialog.show()
}else{
val dialog=AlertDialog.Builder(activity as Context)
dialog.setTitle("Error")
dialog.setMessage("Internet Connection Was Not Found")
dialog.setPositiveButton("ok"){text,listener->
}
dialog.setNegativeButton("Cancel"){text,listener->
}
dialog.create()
dialog.show()
}
}
/*
28-10-21 12.05pm
*/
recyclerdashboard=view.findViewById(R.id.recyclerdashboard)
/*
above cause mistake.be aware of that
*/
/* layoutManager=LinearLayoutManager(activity)
recycleradapter= DashboardRecyclerAdapter(activity as Context,bookinfolist)
recyclerdashboard.adapter=recycleradapter
recyclerdashboard.layoutManager=layoutManager
recyclerdashboard.addItemDecoration(
DividerItemDecoration(
recyclerdashboard.context,
(layoutManager as LinearLayoutManager).orientation
)
)*/
/*
01-11-21 below code
xml directory below res folder
*/
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)
recyclerdashboard.layoutManager=layoutManager
recyclerdashboard.addItemDecoration(
DividerItemDecoration(
recyclerdashboard.context,
(layoutManager as LinearLayoutManager).orientation
)
)
recycleradapter= DashboardRecyclerAdapter(activity as Context,bookinfolist)
recyclerdashboard.adapter=recycleradapter
}
} else {
Toast.makeText(activity as Context, "Some ERROR OCCURED", Toast.LENGTH_SHORT).show()
}
}, Response.ErrorListener {
// here we handel the error
print("error $it")
}) {
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["Content-type"] = "application/json"
headers["token"] = "9bf534118365f1"
return headers
}
}
queue.add(jsonObjectRequest)
return view
}
}
uj5u.com熱心網友回復:
一切都在錯誤訊息中:
lateinit property layoutManager has not been initialized
您已注釋掉 的初始化layoutManager:
layoutManager=LinearLayoutManager(activity)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/345177.html
標籤:安卓 科特林 android-recyclerview
上一篇:在物件陣列中確定與其他陣列元素相比實際物件是否最佳匹配
下一篇:檢查sortedMap中的重復值
