适配器类中的OnClickListener按钮使Kotlin中的应用程序崩溃

时间:2018-10-28 23:46:12

标签: android kotlin

我试图通过按AddtoCartButton将改造后的值存储到数据库中,但按此按钮时应用程序崩溃了。我可以从翻新中检索结果,没有任何问题,但是onclicklistener就是问题。

Code:
  inner class MoviesAdapter : RecyclerView.Adapter<MoviesAdapter.MovieViewHolder>() {
        private val movies: MutableList<Movie> = mutableListOf()
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
            return MovieViewHolder(layoutInflater.inflate(R.layout.item_movie_layout, parent, false))
        }
        override fun getItemCount(): Int {
            return movies.size
        }
        override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
            holder.bindModel(movies[position])
        }
        fun setMovies(data: List<Movie>) {
            movies.addAll(data)
            notifyDataSetChanged()
        }
        inner class MovieViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

            val movieGenreTxt : TextView = itemView.findViewById(R.id.movieGenre)
            val movieYearTxt : TextView = itemView.findViewById(R.id.movieYear)
            val movieAvatarImage : ImageView = itemView.findViewById(R.id.movieAvatar)
            val movieDescription: TextView =itemView.findViewById(R.id.movieDescription)

            fun bindModel(movie: Movie) {
            //    movieTitleTxt.text = movie.name
                movieGenreTxt.text = movie.menu
                movieYearTxt.text = movie.price
                Picasso.get().load(movie.picture).into(movieAvatarImage)
                movieDescription.text=movie.description
                var movieName:String= movie.name!!

               var testvar=movie.name.toString()
            }
        }

    }
fun addtocart(view: View)
{
    Toast.makeText(context,"Success on Click", Toast.LENGTH_SHORT).show()
   print testvar 
}
Error:
  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

1 个答案:

答案 0 :(得分:0)

Below code solves the purpose.
  Code:
      inner class MoviesAdapter : RecyclerView.Adapter<MoviesAdapter.MovieViewHolder>() {
            private val movies: MutableList<Movie> = mutableListOf()
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
                return MovieViewHolder(layoutInflater.inflate(R.layout.item_movie_layout, parent, false))
            }
            override fun getItemCount(): Int {
                return movies.size
            }
            override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
                holder.bindModel(movies[position])
            }
            fun setMovies(data: List<Movie>) {
                movies.addAll(data)
                notifyDataSetChanged()
            }
            inner class MovieViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

                val movieGenreTxt : TextView = itemView.findViewById(R.id.movieGenre)
                val movieYearTxt : TextView = itemView.findViewById(R.id.movieYear)
                val movieAvatarImage : ImageView = itemView.findViewById(R.id.movieAvatar)
                val movieDescription: TextView =itemView.findViewById(R.id.movieDescription)

                fun bindModel(movie: Movie) {
                //    movieTitleTxt.text = movie.name
                    movieGenreTxt.text = movie.menu
                    movieYearTxt.text = movie.price
                    Picasso.get().load(movie.picture).into(movieAvatarImage)
                    movieDescription.text=movie.description
                    var movieName:String= movie.name!!

                   var testvar=movie.name.toString()
                    val addtocart = itemView.findViewById<Button>(R.id.idCart)
                    addtocart!!.setOnClickListener {
                    print $testvar
                        Toast.makeText(context,"Success on Click", Toast.LENGTH_SHORT).show()
                        startActivity(intent) }

                }
            }

        }