英文:
Expecting Member Declaration kotlin androd studio
问题
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
val btn = findViewById<Button>(R.id.mybutton)
btn.text = '点击我'
}
英文:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
val btn = findViewById<Button>(R.id.mybutton)
btn.text = 'click me'
}
it wont run it errors saying expecting member declaration its running android 5.0 as min sdk
I'm trying to change the button text
答案1
得分: 0
你将一些代码放在了 onCreateMethod
外面。你需要将闭合括号移到所有代码之后,像这样:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn = findViewById<Button>(R.id.mybutton)
btn.text = 'click me'
} // <----
}
英文:
You put some of your code outside the onCreateMethod
. You need to move the closing bracket after all your code like this:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn = findViewById<Button>(R.id.mybutton)
btn.text = 'click me'
} // <----
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论