I can Store data to Room Database which I checked through DB Browser. But when i try to view the data in the recycler view it crashes with the error "ecyclerView must not be nul".
The code for Adapter is :
class StdAdapter: RecyclerView.Adapter<StdAdapter.StdViewHolder>() {
private var stdList = emptyList<Student>()
class StdViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StdViewHolder {
return StdViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.std_row, parent, false))
}
override fun onBindViewHolder(holder: StdViewHolder, position: Int) {
val currentItem = stdList[position]
Log.d(TAG, "onBindViewHolder: CurrentItem:$currentItem")
holder.itemView.id_txt.text = currentItem.id.toString()
holder.itemView.viewName_txt.text = currentItem.stdName
holder.itemView.viewClass_txt.text = currentItem.stdClass
holder.itemView.viewRollNum_txt.text = currentItem.rollNum.toString()
Log.d(TAG, "onBindViewHolder: Name: ${currentItem.stdName}")
//Log.d(TAG, "onBindViewHolder: Name: ${currentItem.stdName}")
//Log.d(TAG, "onBindViewHolder: Name: ${currentItem.stdName}")
}
override fun getItemCount(): Int {
return stdList.size
}
fun setData(student: List<Student>){
this.stdList = student
notifyDataSetChanged()
}
}
The code for Fragment in which I am viewing is :
class ViewStdFragment : Fragment() {
@InternalCoroutinesApi
private lateinit var mStdViewModel: StdViewModel
@InternalCoroutinesApi
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_view_std, container, false)
// Recyclerview
val adapter = StdAdapter()
val recyclerView = view.recyclerview
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
// UserViewModel
mStdViewModel = ViewModelProvider(this).get(StdViewModel::class.java)
mStdViewModel.readAllData.observe(viewLifecycleOwner, Observer { student ->
adapter.setData(student)
})
view.addStd_fa.setOnClickListener {
findNavController().navigate(R.id.action_viewStdFragment_to_addStdFragment)
}
return view
}
}
Code for Repository
class StudentRepository(private val studentDao: StudentDao) {
val readAllData: LiveData<List<Student>> = studentDao.readAllData()
suspend fun addStudent(student: Student){
studentDao.addStudent(student)
}
suspend fun updateStudent(student: Student){
studentDao.updateStudent(student)
}
suspend fun deleteStudent(student: Student){
studentDao.deleteStudent(student)
}
suspend fun deleteAllStudents(){
studentDao.deleteAllStudents()
}
}
code for viewmodel is
@InternalCoroutinesApi
class StdViewModel(application: Application): AndroidViewModel(application) {
val readAllData: LiveData<List<Student>>
private val repository: StudentRepository
init {
val studentDao = Userdatabase.getDatabase(
application
).studentDao()
repository = StudentRepository(studentDao)
readAllData = repository.readAllData
}
fun addStudent(student: Student){
viewModelScope.launch(Dispatchers.IO) {
repository.addStudent(student)
}
}
fun updateStudent(student: Student){
viewModelScope.launch(Dispatchers.IO) {
repository.updateStudent(student)
}
}
fun deleteStudent(student: Student){
viewModelScope.launch(Dispatchers.IO) {
repository.deleteStudent(student)
}
}
fun deleteAllStudents(){
viewModelScope.launch(Dispatchers.IO) {
repository.deleteAllStudents()
}
}
}
Code for main Acticity..
class MainActivity3 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main3)
setupActionBarWithNavController(findNavController(R.id.fragment2))
}
/*override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragment2)
return navController.navigateUp() || super.onSupportNavigateUp()
}*/
}
question from:
https://stackoverflow.com/questions/66057305/errorrrecyclerview-must-not-be-null-what-wrong-i-am-doing-in-my-code 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…