Android Studio:未解决的参考:box_three_text
Android Studio: Unresolved reference: box_three_text
我尝试在 Android 中制作练习布局的应用程序,但出现错误:
Unresolved reference: box_three_text
见截图:
[1]: https://i.stack.imgur.com/W3uGt.jpg
为什么讲师代码有效而我的无效?
这是我的代码:
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>
我的系统:Ubuntu 20.04 Android Studio 2021.1.1 Canary 11
该项目正在使用 Kotlin 合成导入。
你需要这个导入MainActivity.kt
import kotlinx.android.synthetic.main.activity_main.*
并检查您的 build.gradle
文件(模块)是否具有 kotlin-android-extensions
插件
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
但是,您所遵循的示例可能已过时,目前推荐Migrate from Kotlin synthetics to Jetpack view binding
您必须在使用变量之前声明它们。您可以使用 databinding / viewbinding / findViewById :
执行以下操作:
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)
}
}
}
我尝试在 Android 中制作练习布局的应用程序,但出现错误:
Unresolved reference: box_three_text
见截图: [1]: https://i.stack.imgur.com/W3uGt.jpg
为什么讲师代码有效而我的无效?
这是我的代码:
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>
我的系统:Ubuntu 20.04 Android Studio 2021.1.1 Canary 11
该项目正在使用 Kotlin 合成导入。
你需要这个导入MainActivity.kt
import kotlinx.android.synthetic.main.activity_main.*
并检查您的 build.gradle
文件(模块)是否具有 kotlin-android-extensions
插件
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
但是,您所遵循的示例可能已过时,目前推荐Migrate from Kotlin synthetics to Jetpack view binding
您必须在使用变量之前声明它们。您可以使用 databinding / viewbinding / findViewById :
执行以下操作:
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)
}
}
}