我有代码将两个文本框中的两个数字加在一起。

    public void sumNumbers(View v){
    EditText input1 = (EditText) findViewById(R.id.input1);
    int calc1 =  Integer.parseInt(String.valueOf(input1));
    EditText input2 = (EditText) findViewById(R.id.input2);
    int calc2 = Integer.parseInt(String.valueOf(input2));
    int total = calc1 + calc2;
    String result = String.valueOf(total);
    EditText output1 = (EditText)findViewById(R.id.output);
    output1.setText(result);
}


但是,当我启动应用程序并按下按钮时,我崩溃了:

Caused by: java.lang.NumberFormatException: Invalid int: "android.support.v7.internal.widget.TintEditText{b412b358 VFED..CL ........ 292,60-392,100 #7f080041 app:id/input1}"
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parse(Integer.java:375)
            at java.lang.Integer.parseInt(Integer.java:366)
            at java.lang.Integer.parseInt(Integer.java:332)
            at com.eren.addingmachine.MainActivity.sumNumbers(MainActivity.java:22)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3628)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)


帮帮我?

最佳答案

请粘贴以下工作代码。我刚刚跑步并检查了一下。

public void sumNumbers(View v){
        EditText input1 = (EditText) findViewById(R.id.input1);
        int calc1 =  Integer.parseInt(String.valueOf(input1.getText()));
        EditText input2 = (EditText) findViewById(R.id.input2);
        int calc2 = Integer.parseInt(String.valueOf(input2.getText()));
        int total = calc1 + calc2;
        String result = String.valueOf(total);
        EditText output1 = (EditText)findViewById(R.id.output);
        output1.setText(result);
    }

关于java - java.lang.NumberFormatException:无效的int:“android.support.v7.internal.widget.TintEditText,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28047367/

10-10 08:39