英文:
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<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 thintent 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
答案1
得分: 0
以下是翻译好的部分:
"实际上是什么导致了崩溃?包含按钮的 Activity,还是在点击按钮后的 RegisterActivity
?你的错误(请将堆栈跟踪作为文本在代码块中发布!不要以图像形式呈现文本)说一个 Activity 在尝试在 Window
之前调用 findViewById
时失败,这意味着你正在构建时尝试这样做,就像这样的顶级变量一样:
class SomeActivity : AppCompatActivity() {
val badIdea = findViewById<Whatever>(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<Whatever>(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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论