简体   繁体   中英

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

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

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

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. 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.

What do you expect this line to do? Why do you expect querying a button's text to return an integer that represents age?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM