[英]kotlin app crashes when going one to another activity
i have 3 activitiy when i start my app i can go 1 activity to second but i cant go third it crashes当我启动我的应用程序时我有 3 个活动我可以 go 1 个活动到第二个但我不能 go 第三个它崩溃
this is code of my third( last activity )这是我第三次(最后一次活动)的代码
class thirdActivity3 : AppCompatActivity() {
private lateinit var textView: TextView
private lateinit var textView2: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_third3)
textView = findViewById(R.id.textView)
textView2 = findViewById(R.id.textView2)
val extras = intent.extras
if(extras != null){
textView.text = extras.getString("NAME") //getString("NAME","")
textView2.text = extras.getInt("AGE").toString() //getInt("AGE",0).toString()
}
}
}
and this is my second activity这是我的第二个活动
class secondActivity2 : AppCompatActivity() {
private lateinit var inputTextAge:EditText
private lateinit var finishButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second2)
inputTextAge=findViewById(R.id.inputTextAge)
finishButton=findViewById(R.id.finishButton)
val extras = intent.extras
var name = ""
if (extras != null){
name=extras.getString("PERSON_NAME","")
}
finishButton.setOnClickListener {
val age = finishButton.text.toString().toInt()
val intent = Intent(this,thirdActivity3::class.java)
intent.putExtra("NAME",name)
intent.putExtra("AGE",age)
startActivity(intent)
}
}
}
i am trying to make wizard for learning but my code have some problems.我正在尝试制作学习向导,但我的代码有一些问题。 i have 3 activitys in kotling but when i go second activity to third it's stopping but its workd when i going first to second我在科特林有 3 项活动,但是当我 go 第二次活动到第三次时它停止了,但是当我从第一到第二次时它起作用了
2020-11-27 18:50:41.472 19343-19343/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 19343
java.lang.NumberFormatException: For input string: "Finish"
at java.lang.Integer.parseInt(Integer.java:615)
at java.lang.Integer.parseInt(Integer.java:650)
at com.example.myapplication.secondActivity2$onCreate$1.onClick(secondActivity2.kt:33)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
this is from logcat这是来自 logcat
Looking at the error message, it looks like your problem is this line:查看错误消息,您的问题似乎是这一行:
val age = finishButton.text.toString().toInt()
The error is saying that you're trying to parse the string "Finish" as a number.错误是说您正在尝试将字符串“Finish”解析为数字。 It makes sense from this line that you're doing that.从这条线来看,你正在这样做是有道理的。 You have a button named "Finish".您有一个名为“完成”的按钮。 You get the text
from the button and then call toInt()
on that text, and you get this error.您从按钮获取text
,然后对该文本调用toInt()
,然后您会收到此错误。
What do you expect this line to do?你希望这条线做什么? Why do you expect querying a button's text to return an integer that represents age?为什么您期望查询按钮的文本会返回代表年龄的 integer?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.