I thought when response.body()?.let {}
run, enrollBookAdapter
(or booklist = it.books as mutableList<book>
) would be initialized but, it did not... What am I doing wrong? How can I initailize bookList with Books...
class EnrollBookActivity : AppCompatActivity() {
private lateinit var binding: ActivityEnrollBookBinding
private lateinit var bookService: BookService
private lateinit var bookList:MutableList<Book>
private lateinit var enrollBookAdapter: EnrollBookAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityEnrollBookBinding.inflate(layoutInflater)
setContentView(binding.root)
initBookService()
bookServiceLoadBestSellers()
binding.selfBtn.setOnClickListener{
startActivity(Intent(this,SelfWriteActivity::class.java))
}
enrollBookAdapter.setOnBookClickListener(object:EnrollBookAdapter.OnBookClickListener{
override fun onBookClick(position: Int, book: Book) {
val intent = Intent(this@EnrollBookActivity,TalerMain::class.java)
intent.putExtra("title",enrollBookAdapter.bookList[position].title)
intent.putExtra("url",enrollBookAdapter.bookList[position].title)
startActivity(intent)
}
})
}
private fun initBookRecyclerView(bookList: MutableList<Book>) {
enrollBookAdapter = EnrollBookAdapter(bookList)
enrollBookAdapter.setOnBookClickListener(object:EnrollBookAdapter.OnBookClickListener{
override fun onBookClick(position: Int, book: Book) {
val intent = Intent(this@EnrollBookActivity,TalerMain::class.java)
intent.putExtra("title",enrollBookAdapter.bookList[position].title)
intent.putExtra("url",enrollBookAdapter.bookList[position].title)
startActivity(intent)
}
})
binding.bookRv.layoutManager = GridLayoutManager(this,2)
binding.bookRv.adapter = EnrollBookAdapter(bookList)
binding.bookRv.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL))
}
private fun bookServiceLoadBestSellers() {
bookService.getBestSellerBooks(getString(R.string.interparkAPIKey))
.enqueue(object : Callback<BestSellerDto> {
override fun onResponse(
call: Call<BestSellerDto>,
response: Response<BestSellerDto>
) {
if (response.isSuccessful.not()) {
Log.e(M_TAG, "NOT!! SUCCESS")
return
}
response.body()?.let {
Log.d(M_TAG, it.toString())
it.books.forEach { book ->
Log.d(M_TAG, book.toString())
}
//enrollBookAdapter = EnrollBookAdapter(it.books as MutableList<Book>)
initBookRecyclerView(it.books as MutableList<Book>)
//setRecyclerView(it.books)
}
}
override fun onFailure(call: Call<BestSellerDto>, t: Throwable) {
Log.e(M_TAG, t.toString())
}
})
}
}
Error
"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.clone.practice/com.clone.practice.EnrollBookActivity}: kotlin.UninitializedPropertyAccessException: lateinit property enrollBookAdapter has not been initialized"
I thought when response.body()?.let {}
run, enrollBookAdapter
(or booklist = it.books as mutableList<book>
) would be initialized but, it did not... What am I doing wrong? How can I initialize bookList with Books...
CodePudding user response:
Try to add initBookRecyclerView
before enrollBookAdapter.setOnBookClickListener(object:EnrollBookAdapter.OnBookClickListener
line in onCreate
.
After that uncomment enrollBookAdapter = EnrollBookAdapter(it.books as MutableList<Book>)
and comment the next line.
CodePudding user response:
just remove this code from your onCreate
method only as you are trying to use enrollBookAdapter
variable before initialisation which is causing crash .
And you have already set this method in initBookRecyclerView
method so no need in onCreate
method
enrollBookAdapter.setOnBookClickListener(object:EnrollBookAdapter.OnBookClickListener{
override fun onBookClick(position: Int, book: Book) {
val intent = Intent(this@EnrollBookActivity,TalerMain::class.java)
intent.putExtra("title",enrollBookAdapter.bookList[position].title)
intent.putExtra("url",enrollBookAdapter.bookList[position].title)
startActivity(intent)
}
})