I try to make an application to practice Layout in Android, but I get an error:
Unresolved reference: box_three_text
See screenshot: [1]: https://i.stack.imgur.com/W3uGt.jpg
Why does the instructor code work and mine doesn't?
Here's my code:
MainActivity.kt
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setListeners()
}
private fun setListeners(){
val clickableViews: List<View> =
listOf(box_one_text, box_two_text, box_three_text, box_four_text, box_five_text)
for (item in clickableViews){
item.setOnClickListener{makeColored(it)}
}
}
fun makeColored(view: View) {
when (view.id) {
// Boxes using Color class colors for background
R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
// Boxes using Android color resources for background
R.id.box_three_text -> view.setBackgroundResource(android.R.color.holo_green_light)
R.id.box_four_text -> view.setBackgroundResource(android.R.color.holo_green_dark)
R.id.box_five_text -> view.setBackgroundResource(android.R.color.holo_green_light)
else -> view.setBackgroundColor(Color.LTGRAY)
}
}
}
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:id="@ id/constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@ id/box_one_text"
style="@style/WhiteBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_wide"
android:layout_marginTop="@dimen/margin_wide"
android:layout_marginEnd="@dimen/margin_wide"
android:fontFamily="@font/roboto"
android:text="@string/box_one"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/box_two_text"
style="@style/WhiteBox"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="@string/box_two"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/box_one_text" />
<TextView
android:id="@ id/box_three_text"
style="@style/WhiteBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/box_three"
app:layout_constraintBottom_toTopOf="@ id/box_four_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@ id/box_two_text"
app:layout_constraintTop_toTopOf="@ id/box_two_text" />
<TextView
android:id="@ id/box_four_text"
style="@style/WhiteBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="@string/box_four"
app:layout_constraintBottom_toTopOf="@ id/box_five_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@ id/box_two_text"
app:layout_constraintTop_toBottomOf="@ id/box_three_text" />
<TextView
android:id="@ id/box_five_text"
style="@style/WhiteBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="@string/box_five"
app:layout_constraintBottom_toBottomOf="@ id/box_two_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@ id/box_two_text"
app:layout_constraintTop_toBottomOf="@ id/box_four_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
My system: Ubuntu 20.04 Android Studio 2021.1.1 Canary 11
CodePudding user response:
You have to declare the variables before using them . You can either use databinding / viewbinding / findViewById for the same :
Do the following :
class MainActivity : AppCompatActivity() {
private lateinit var box_one_text : TextView
private lateinit var box_two_text : TextView
private lateinit var box_three_text : TextView
private lateinit var box_four_text : TextView
private lateinit var box_five_text : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
box_one_text = findViewById(R.id.box_one_text)
box_two_text = findViewById(R.id.box_two_text)
box_three_text = findViewById(R.id.box_three_text)
box_four_text = findViewById(R.id.box_four_text)
box_five_text = findViewById(R.id.box_five_text)
setListeners()
}
private fun setListeners(){
val clickableViews: List<View> =
listOf(box_one_text, box_two_text, box_three_text, box_four_text, box_five_text)
for (item in clickableViews){
item.setOnClickListener{makeColored(it)}
}
}
fun makeColored(view: View) {
when (view.id) {
// Boxes using Color class colors for background
R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
// Boxes using Android color resources for background
R.id.box_three_text -> view.setBackgroundResource(android.R.color.holo_green_light)
R.id.box_four_text -> view.setBackgroundResource(android.R.color.holo_green_dark)
R.id.box_five_text -> view.setBackgroundResource(android.R.color.holo_green_light)
else -> view.setBackgroundColor(Color.LTGRAY)
}
}
}
CodePudding user response:
The project is using Kotlin synthetics imports.
You need this import in you MainActivity.kt
import kotlinx.android.synthetic.main.activity_main.*
and check that your build.gradle
file (Module) has the kotlin-android-extensions
plugin
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
However, the example you are following is probably outdated, it is currently recommended Migrate from Kotlin synthetics to Jetpack view binding