Kotlin按钮在尝试切换活动时导致崩溃?

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

Kotlin button causing crash when trying to switch activity?

问题

I am trying to use a button to go from a sign in page to a register page but for some reason it isn't working. From my understanding this process should be relatively simple but for some reason the action doesn't function as hoped.

I've tried using intent as I have for other pages but it just crashes the app (see Example 1). I do have another function on the same page using intent, is this an issue.

Example 1

val goToRegisterButton = findViewById<Button>(R.id.goToRegisterButton)
goToRegisterButton.setOnClickListener {
    Toast.makeText(this, "Button Click!", Toast.LENGTH_SHORT).show()
    val regIntent = Intent(this, RegisterActivity::class.java)
    startActivity(regIntent)
}

I then tried calling a function which would do the intent side of things but again no luck (See Example 2).

Example 2

val goToRegisterButton = findViewById<Button>(R.id.goToRegisterButton)
goToRegisterButton.setOnClickListener {
    Toast.makeText(this, "Button Click!", Toast.LENGTH_SHORT).show()
    goRegisterActivity()
}
private fun goRegisterActivity() {
    Log.i(TAG, "goRegisterActivity")
    val regIntent = Intent(this, RegisterActivity::class.java)
    startActivity(regIntent)
}

Edit: I was asked to add the stack trace, I think this is it.
stack trace of issue

英文:

I am trying to use a button to go to from a sign in page to a register page but for some reason it isn't working. From my understanding this process should be relatively simple but for some reason the action doesn't function as hoped.

I've tried using intent as I have for other pages but it just crashes the app (see Example 1). I do have another function on the same page using intent, is this an issue.

Example 1

        val goToRegisterButton = findViewById&lt;Button&gt;(R.id.goToRegisterButton)
        goToRegisterButton.setOnClickListener {
            Toast.makeText(this, &quot;Button Click!&quot;, Toast.LENGTH_SHORT).show()
            val regIntent = Intent(this, RegisterActivity::class.java)
            startActivity(regIntent)
        }

I then tried calling a function which would do thintent side of things but again no luck (See Example 2).

Example 2

        val goToRegisterButton = findViewById&lt;Button&gt;(R.id.goToRegisterButton)
        goToRegisterButton.setOnClickListener {
            Toast.makeText(this, &quot;Button Click!&quot;, Toast.LENGTH_SHORT).show()
            goRegisterActivity()
            
        }
    private fun goRegisterActivity() {
        Log.i(TAG, &quot;goRegisterActivity&quot;)
        val regIntent = Intent(this, RegisterActivity::class.java)
        startActivity(regIntent)
    }

Edit: I was asked to add the stack trace, I think this is it.
stack trace of issue

答案1

得分: 0

以下是翻译好的部分:

"实际上是什么导致了崩溃?包含按钮的 Activity,还是在点击按钮后的 RegisterActivity?你的错误(请将堆栈跟踪作为文本在代码块中发布!不要以图像形式呈现文本)说一个 Activity 在尝试在 Window 之前调用 findViewById 时失败,这意味着你正在构建时尝试这样做,就像这样的顶级变量一样:

class SomeActivity : AppCompatActivity() {
    val badIdea = findViewById&lt;Whatever&gt;(R.id.some_id)
}

你不能这样做,因为在调用 setContentView 之前没有视图层次结构(因此没有视图可查找),而在构建时甚至没有 Context 以调用它,这就是为什么你会遇到崩溃的原因。视图查找必须在 onCreate 或以后的时间完成,当你的 Activity 与一个 Context 关联时。"

英文:

What's actually crashing? The Activity containing the button, or RegisterActivity after you click the button? Your error (please post the stacktrace as text in a code block next time! No text as images) says an Activity is failing to start because of an attempt to call findViewById before a Window, and that implies you're doing it at construction time, in a top-level variable like this:

class SomeActivity : AppCompatActivity() {
    val badIdea = findViewById&lt;Whatever&gt;(R.id.some_id)
}

You can't do that because you don't have a view hierarchy until setContentView is called (so there's no view to find), and at construction time you don't have a Context to even call it on, which is why you're getting a crash. View lookups have to be done in onCreate or later, when your Activity is associated with a Context

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

发表评论

匿名网友

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

确定