我认为当response.body()?.let {}
运行时,enrollBookAdapter
(或booklist = it.books as mutableList<book>
)将被初始化,但是,它没有…我做错了什么?
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())
}
})
}
}
误差
"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.clone.practice/com.clone.practice.EnrollBookActivity}: kotlin.UninitializedPropertyAccessException: lateinit property enrollBookAdapter has not been initialized"
我认为当response.body()?.let {}
运行时,enrollBookAdapter
(或booklist = it.books as mutableList<book>
)将被初始化,但是,它没有…我做错了什么?
尝试在onCreate
的enrollBookAdapter.setOnBookClickListener(object:EnrollBookAdapter.OnBookClickListener
行之前添加initBookRecyclerView
。
之后取消注释enrollBookAdapter = EnrollBookAdapter(it.books as MutableList<Book>)
并注释下一行。
仅当您试图在初始化之前使用导致崩溃的enrollBookAdapter
变量时,从onCreate
方法中删除此代码。而且你已经在initBookRecyclerView
方法中设置了这个方法所以不需要在onCreate
方法中设置
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)
}
})