Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
Object in ViewHolder getter/setter required on Kotlin

not sure what is going on. This worked fine in Java but when switching to Kotlin I don't really know how to make this work. I get this error But I get the same error which is

Property getter or setter required on the val mMoment: Moment

class SquareViewHolder(v: CardView, viewModel: BarreViewModel) : RecyclerView.ViewHolder (v) {

    val mImage: ImageView
    val by lazy mMoment: Moment


    init {

        mImage = v.findViewById(R.id.square_moment)

        mImage.setOnClickListener {
            //val focused = ArrayList<Moment>(0)
            //focused.add(mMoment)

            //viewModel.focusedMoments = focused
            //viewModel.setItemClicked(true)
        }
    }



    fun bind(moment: Moment) {
        val mMoment = moment
    }
}

I have also tried var lateinit mMoment: Moment But I receive the same error.

Now, the moment object is a Kotlin Data Object, I'm not sure if that would cause a problem.

This worked on java, but for some reason kotlin doesn't seem to like the instantiation of the object this way. I don't know why. Thanks for any help!

In Java it was:

public class SquareMomentViewHolder extends RecyclerView.ViewHolder {

public ImageView mImage;
public Moment mMoment;

BarreChatViewModel viewModel;

public SquareMomentViewHolder(CardView v, BarreChatViewModel viewModel) {
    super(v);
    mImage = v.findViewById(R.id.square_moment);

    mImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            List<Moment> focused = new ArrayList(0);
            focused.add(mMoment);
            viewModel.setFocusedMoments(focused);

            viewModel.setItemClicked(true);
        }
    });
}

public void bind(Moment moment) {
    mMoment = moment;
}

}

A possible cause of this error might be that this dataclass is used for my ROOMDatabase. It's an entity object I was dragging to the User Interface. Maybe this isn't a proper way to do things. I don't know.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Lazy is where it will initialise the variable only when it is used, but it requires some code when declaring it as lazy.

E.g. val mMoment by lazy { // Some code that generates this }

As your variable will be initialised in a bind, you should do:

lateinit var mMoment: Moment

This means that it will get initialised later on. If you try to use it without it being initialised, your program will throw an exception.

Or if this will potentially never get bound, you should initialise it as a nullable value:

var mMoment: Moment? = null

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda