Android:代码运行时错误

时间:2016-08-29 14:38:52

标签: java android eclipse

我正在写一个简单的计算器,但不幸的是它没有用。

LogCat说错误发生在我的at android.view.View$1.onClick(View.java:2072)

activity_main.xml

但我很遗憾地说我无法找到它。所以请帮助我。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="54dp" android:layout_marginTop="60dp" android:ems="10" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="74dp" android:ems="10" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_alignParentTop="true" android:layout_marginTop="38dp" android:text="Input first number" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText1" android:layout_marginTop="36dp" android:text="Input 2nd number" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="functionCalculator" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="26dp" android:text="+"/> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" android:layout_marginTop="28dp" android:text="Your result here" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView3" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/button1" android:text="Result" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button1" android:layout_alignRight="@+id/textView4" android:text="-" android:onClick="functionCalculator" /> </RelativeLayout>

ActivityMain.java

package com.example.caculator; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { EditText e1,e2; TextView t1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); e1= (EditText)findViewById(R.id.editText1); e2 = (EditText)findViewById(R.id.editText2); t1 = (TextView)findViewById(R.id.textView4); } public void functionCalculator(View view) { int number1 , number2 , result; if (view.getId()==R.id.button1) { number1 = Integer.parseInt(e1.getText().toString()); number2 = Integer.parseInt(e2.getText().toString()); result = number1+number2; t1.setText(Integer.toString(result)); } if (view.getId()==R.id.button2) { number1 = Integer.parseInt(e1.getText().toString()); number2 = Integer.parseInt(e2.getText().toString()); result = number1-number2; t1.setText(Integer.toString(result)); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }

{{1}}

1 个答案:

答案 0 :(得分:0)

我运行了您的代码,只要我将整数放在e1e2中,就不会遇到任何问题。一旦我输入十进制数字,我就遇到了与你提到的完全相同的问题。这是因为您试图从输入中解析整数,这些整数可能并不总是成功并且将导致NumberFormatException。您应该编写代码,以便它不会崩溃,无论如何。假设您的用户是白痴。尝试下面的代码,它不应该崩溃:

public void functionCalculator(View view) {

    try {
        // Firstly checking for integers
        int number1, number2;
        number1 = Integer.parseInt(e1.getText().toString());
        number2 = Integer.parseInt(e2.getText().toString());

        if (view.getId() == R.id.button1)
            t1.setText(Integer.toString(number1 + number2));

        else if (view.getId() == R.id.button2)
            t1.setText(Integer.toString(number1 - number2));

    } catch (NumberFormatException e) {
        float number1, number2;

        try {
            // Now checking for decimals
            number1 = Float.parseFloat(e1.getText().toString());
            number2 = Float.parseFloat(e2.getText().toString());

            if (view.getId() == R.id.button1)
                t1.setText(Float.toString(number1 + number2));

            else if (view.getId() == R.id.button2)
                t1.setText(Float.toString(number1 - number2));

        } catch (NumberFormatException e2) {
            // No numbers :(
            Toast.makeText(this, "WTF! Enter number here you dingus!", Toast.LENGTH_SHORT).show();
        }
    }

}