【发布时间】:2023-12-23 06:30:01
【问题描述】:
该怎么做? 我在这里声明了 val,这样我就可以在所有函数中使用 val e11 但它会崩溃,为什么?
//val e11=findViewById<EditText>(R.id.e1)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_lifecycle)
val b11=findViewById<Button>(R.id.b1)
b11.setOnClickListener{
startActivity(Intent(this,another::class.java))
}}
override fun onStart() {
super.onStart()
Toast.makeText(this, "am started", Toast.LENGTH_SHORT).show()
}override fun onResume() {
super.onResume()
Toast.makeText(this, "am resumed", Toast.LENGTH_SHORT).show()
}
override fun onPause() {
super.onPause()
val e1=findViewById<EditText>(R.id.e1)
e1.setText("")
}
}```
【问题讨论】:
-
这是因为您试图在创建视图之前查找视图 ID。 //val e11=findViewById
(R.id.e1)
标签: android android-studio kotlin declaration kotlin-android-extensions