致命的例外!科特林

时间:2018-03-25 07:45:27

标签: android kotlin

我试图制作一个计算价格的程序,用户通过单选按钮选择纸张类型,尺寸,打印颜色并输入多少纸张和副本,我得到了这个运行时错误:

  

E / AndroidRuntime:致命异常:主要                     过程:com.printonejuc.sarah.test,PID:8534                     java.lang.NumberFormatException:对于输入字符串:“android.support.v7.widget.AppCompatEditText {c260af3 VFED..CL。......我。0,0-0,0#7f090009 app:id / Pages} “                         在java.lang.Integer.parseInt(Integer.java:608)                         在java.lang.Integer.parseInt(Integer.java:643)                         在com.printonejuc.sarah.test.newOrder $ onCreate $ 1.onClick(newOrder.kt:74)                         在android.view.View.performClick(View.java:6256)                         在android.view.View $ PerformClick.run(View.java:24701)                         在android.os.Handler.handleCallback(Handler.java:789)                         在android.os.Handler.dispatchMessage(Handler.java:98)                         在android.os.Looper.loop(Looper.java:164)                         在android.app.ActivityThread.main(ActivityThread.java:6541)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

newOrder.kt

resultTxt.setOnClickListener {

        if ((PaperSize.checkedRadioButtonId != -1) && (PaperSize.checkedRadioButtonId != -1) && (PaperType.checkedRadioButtonId != -1)) {

            if (A4size.isChecked) {
                if (Plain.isChecked && Black.isChecked) {
                    //plain black calculation
                    totalPrice =(1 * PagesI.toInt())*CopiesI.toInt()
                   result.text = "total:  " + totalPrice

                }

第74行:

  

totalPrice =(1 * PagesI.toInt())* CopiesI.toInt()

页面和副本在onCreat方法内部和onClickListner

之前初始化
 Pages = findViewById<View>(R.id.Pages)as EditText
    Copies = findViewById<View>(R.id.Copies) as EditText
    var PagesI = Pages.toString()
    var CopiesI = Copies.toString()

1 个答案:

答案 0 :(得分:1)

看起来PagesI和/或CopiesI是某种EditText小部件。

您不应该在窗口小部件上调用toInt(),而应该调用窗口小部件中包含的文本。在.text之前添加.toInt()

totalPrice =(1 * PagesI.text.toInt())*CopiesI.text.toInt()