0

I am new in using Android Studio. I used this because our professor wants us to make an application. I am following the tutorial on YouTube step by step and I know that I did everything he did on the video. But why am i having this error Unresolved reference: nextActivity?

Here is the code in MainActivity.kt

package com.willitpush.app

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        nextActivity.setOnClickListener{

        }
    }
}

Here is the code on activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/nextActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="#2196F3"
        android:text="play"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

As you can see there should be no error because I typed in the same id. but why am I having error on this line? nextActivity.setOnClickListener. Sorry I am just new in Android Studio.

3

1 Answer 1

2

Unresolved reference means you're referring to something that doesn't exist, like an undeclared variable name - or at least the compiler doesn't know about it, from the code that you have. nextActivity isn't declared in your code anywhere, so the compiler is saying "ok, what's this?"

What Stanislav is showing you is the plugin that enables "Kotlin synthetic binding", which is basically magic code that turns your declared IDs in the layout XML file (like android:id="@+id/nextActivity" in your Button) into a variable in your Activity class with the same name as the ID. So even though you haven't declared a val nextActivity: Button anywhere, the synthetics have added it, so you can refer to it like in your example. Without synthetics, it won't work.

Don't worry about not understanding all that, this kind of magic is great when you know it's there and how it works, it's incredibly confusing for a beginner. The normal way to look up a Button (or any View) is to do

val nextActivity = findViewById<Button>(R.id.nextActivity)

Kotlin synthetics are deprecated now, you're meant to use the ViewBinding library instead. Honestly, I'd use findViewById if you're new so that you get used to the general idea of things - you'll learn a lot! You can use the fancy automation later

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.