未解决的参考:section_label
Unresolved reference: section_label
我正在学习 android 使用 KOTLIN 进行应用程序开发并收到错误 Unresolved reference。错误意味着我指的是不存在的东西,比如未声明的变量名。但是我的布局 XML 文件确实包含带有标签的 id。有人可以让我知道我错过了什么吗?
这是我的 MainActivity.kt 文件
package com.example.chapapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
class PlaceholderFragment:Fragment(){
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_main,container,false)
rootView.section_label.text = "Hello World from section ${arguments?.getInt(ARG_SECTION_NUMBER)}"
return rootView
}
companion object {
private val ARG_SECTION_NUMBER = "Section number"
fun newIntent(sectionNumber: Int): PlaceholderFragment {
val fragment = PlaceholderFragment()
val args = Bundle()
args.putInt(ARG_SECTION_NUMBER, sectionNumber)
fragment.arguments = args
return fragment
}
}
}
}
fragment_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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
rootView.section_label.text
在这里,您尝试使用 Kotlin Android 扩展插件提供的合成视图绑定来访问视图。
此插件现已弃用,因此您可以使用 findViewById()
:
val section_label = rootView.findViewById<TextView>(R.id.section_label)
section_label.text = "Hello World from section ${arguments?.getInt(ARG_SECTION_NUMBER)}"
或者,如果您想使用已弃用的插件,您需要确保:
在 build.gradle(模块级别)中添加 Kotlin Android 扩展插件
plugins {
id 'kotlin-android-extensions'
}
然后重建您的项目,并确保在 MainActivity
中导入合成库,例如:
import kotlinx.android.synthetic.main.activity_main.*
我正在学习 android 使用 KOTLIN 进行应用程序开发并收到错误 Unresolved reference。错误意味着我指的是不存在的东西,比如未声明的变量名。但是我的布局 XML 文件确实包含带有标签的 id。有人可以让我知道我错过了什么吗?
这是我的 MainActivity.kt 文件
package com.example.chapapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
class PlaceholderFragment:Fragment(){
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_main,container,false)
rootView.section_label.text = "Hello World from section ${arguments?.getInt(ARG_SECTION_NUMBER)}"
return rootView
}
companion object {
private val ARG_SECTION_NUMBER = "Section number"
fun newIntent(sectionNumber: Int): PlaceholderFragment {
val fragment = PlaceholderFragment()
val args = Bundle()
args.putInt(ARG_SECTION_NUMBER, sectionNumber)
fragment.arguments = args
return fragment
}
}
}
}
fragment_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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
rootView.section_label.text
在这里,您尝试使用 Kotlin Android 扩展插件提供的合成视图绑定来访问视图。
此插件现已弃用,因此您可以使用 findViewById()
:
val section_label = rootView.findViewById<TextView>(R.id.section_label)
section_label.text = "Hello World from section ${arguments?.getInt(ARG_SECTION_NUMBER)}"
或者,如果您想使用已弃用的插件,您需要确保:
在 build.gradle(模块级别)中添加 Kotlin Android 扩展插件
plugins {
id 'kotlin-android-extensions'
}
然后重建您的项目,并确保在 MainActivity
中导入合成库,例如:
import kotlinx.android.synthetic.main.activity_main.*