Home > Software engineering >  Type mismatch when setting startActivity() in Kotlin
Type mismatch when setting startActivity() in Kotlin

Time:12-05

I'm new on Kotlin for programming Android mobile. I want to try to make my own movie list for my experiment from many tutorials and for Android mobile programming learning.

So, I want to try to make Explicit Intent that started from movie lists that one of them are clicked into an movie information that I clicked.

After I get movie datasets from JSON in MainActivity.kt, I use the code below in MainActivity.kt to MovieAdapter class (MovieAdapter.kt)

val customAdapter = MovieAdapter(moviePoster, movieTitle, movieYear, movieGenre, movieDirectors, movieRunTime, movieRating, movieActors, movieOverview, this@MainActivity)
recyclerView.setAdapter(customAdapter)

And this is the MovieAdapter class (MovieAdapter.kt)

class MovieAdapter(var moviePoster: ArrayList<String>,
                   var movieTitle: ArrayList<String>,
                   var movieYear: ArrayList<String>,
                   var movieGenre: ArrayList<String>,
                   var movieDirectors: ArrayList<String>,
                   var movieRunTime: ArrayList<String>,
                   var movieRating: ArrayList<String>,
                   var movieActors: ArrayList<String>,
                   var movieOverview: ArrayList<String>, var ctx: Context) : RecyclerView.Adapter<MovieAdapter.MyViewHolder>()

Then in MovieAdapter.kt (MovieAdapter class) like in the code below

override fun onBindViewHolder(holder: MyViewHolder, position: Int)
{
    holder.moviePoster.tag = moviePoster[position]
    holder.movieTitle.text = movieTitle[position]
    holder.movieYear.text = movieYear[position]
    holder.movieGenre.text = movieGenre[position]


    val moviePosterDetail = moviePoster[position]
    val movieTitleDetail = movieTitle[position]
    val movieYearDetail = movieYear[position]
    val movieGenreDetail = movieGenre[position]
    val movieDirectorDetail = movieDirectors[position]
    val movieTimeDetail = movieRunTime[position]
    val movieRatingDetail = movieRating[position]
    val movieActorsDetail = movieActors[position]
    val movieDescriptionDetail = movieOverview[position]


    holder.itemView.setOnClickListener{

        Intent(ctx, MovieDetail::class.java).also {

            it.putExtra("moviePoster", moviePosterDetail)
            it.putExtra("movieTitle", movieTitleDetail)
            it.putExtra("movieYear", movieYearDetail)
            it.putExtra("movieGenre", movieGenreDetail)
            it.putExtra("movieDirector", movieDirectorDetail)
            it.putExtra("movieTime", movieTimeDetail)
            it.putExtra("movieRating", movieRatingDetail)
            it.putExtra("movieActors", movieActorsDetail)
            it.putExtra("movieDescription", movieDescriptionDetail)

            startActivity(it)

        }

    }
}

In the startActivity(it) part, there is a error in below

Type mismatch. Required: Context Found: Intent No value passed for parameter 'intent' No value passed for parameter 'options'

But in other files when do startActivity(it) part (in MainActivity.kt file), no error detected here.

So, why it is happened and how can I do this? And if I must to use context and options parameters in startActivity(), what should I do to fill the parameters in startActivity() function?

CodePudding user response:

I'm not able to run an XML/View based project atm, but the startActivity you are calling might be the ContextCompat or something.

Since the name of your parameter is

var ctx: Context

where you passed the instance of your activity

this@MainActivity

Can you try using the ctx parameter calling the startActivity instead?

Intent(ctx, MovieDetail::class.java).also {

    ...
    ...

    ctx.startActivity(it)
}
  • Related