Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

kotlin - Errorr:recyclerView must not be null,what wrong I am doing in my code?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A NullPointerException when using a synthetic view property indicates that the requested view doesn't exist in the layout you are searching. In this case, it means R.layout.fragment_view_std.xml does not contain a view with the id recyclerView.

Synthetic view references (kotlin-android-extensions) are deprecated primarily for this reason (unsafe properties with poor error messaging). View binding should be preferred.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...