Expecting Member Declaration kotlin androd studio

huangapple go评论86阅读模式
英文:

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&lt;Button&gt;(R.id.mybutton)
    btn.text = &#39;click me&#39;
}

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&lt;Button&gt;(R.id.mybutton)
        btn.text = &#39;click me&#39;
    } // &lt;----
}

huangapple
  • 本文由 发表于 2023年3月7日 09:00:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定