点击按钮导致崩溃的应用程序

时间:2016-01-15 19:31:59

标签: java android android-layout

我创建了一个简单的Android应用程序,它带两个数字并通过添加显示它们。当我输入值时,它可以正常工作,但如果我没有输入值并按下按钮,应用程序会崩溃。请指导我并提前感谢 这是它的XML代码

<EditText
    android:id="@+id/et_first"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter first number"
    android:inputType="number" />

<EditText
    android:id="@+id/et_second"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et_first"
    android:hint="Enter second number"
    android:inputType="number" />

<Button
    android:id="@+id/btn_add"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:onClick="addition"
    android:text="+" />

<TextView
    android:id="@+id/tv_result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btn_add"
    android:textAppearance="?android:textAppearanceLarge" />     

这是它的MainActivity.Java代码

 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.TextView;

 public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void addition(View view) {
    EditText first_number = (EditText) findViewById(R.id.et_first);
    float first = Float.parseFloat(first_number.getText().toString());
    EditText second_number = (EditText) findViewById(R.id.et_second);
    float second = Float.parseFloat(second_number.getText().toString());
    TextView tv_result = (TextView) findViewById(R.id.tv_result);
    float result = first + second;
    tv_result.setText(String.valueOf(result));
}
 }

4 个答案:

答案 0 :(得分:1)

您需要首先检查EditText是否确实包含文本,否则当您尝试将空字符串解析为浮点数时,您将获得NumberFormatException

String firstNumber = first_number.getText().toString();
float first = TextUtils.isEmpty(firstNumber) ? 0 : Float.parseFloat(firstNumber);

如果您还想处理评论中提到的无效输入,请尝试以下方法:

private float parseFloat(String floatStr) {
    if(TextUtils.isEmpty(floatStr)) return 0;

    try {
         return Float.parseFloat(floatStr);
    } catch(NumberFormatException e) {
        return 0;
    }
}

您可以将0替换为您想要使用的任何其他默认值。 然后,您可以执行以下操作:
float first = parseFloat(first_number.getText().toString());

答案 1 :(得分:0)

你需要写这样的东西:

       float first = 0.0, second = 0.0;
        EditText first_number = (EditText) findViewById(R.id.et_first);
        if(first_number.getText() != null && first_number.getText().length() > 0){ 
          first = Float.parseFloat(first_number.getText().toString());
        }
        if(second_number.getText() != null && second_number.getText().length() > 0){ 
          first = Float.parseFloat(first_number.getText().toString());
        }
        EditText second_number = (EditText) findViewById(R.id.et_second);
        float second = Float.parseFloat(second_number.getText().toString());
        TextView tv_result = (TextView) findViewById(R.id.tv_result);
        float result = first + second;
        tv_result.setText(String.valueOf(result));

答案 2 :(得分:0)

很简单,当你没有输入任何数字并且仍然按下添加按钮时它会崩溃,因为它会在“空白”上执行添加。和应用程序将崩溃。

public void addition(View view) {

    EditText first_number = (EditText) findViewById(R.id.et_first);

    if(!first_number.equals("") && first_number!=null)
     float first = Float.parseFloat(first_number.getText().toString());

    EditText second_number = (EditText) findViewById(R.id.et_second);
    if(!second_number .equals("") && second_number !=null)
     float second = Float.parseFloat(second_number.getText().toString());

    TextView tv_result = (TextView) findViewById(R.id.tv_result);
    float result = first + second;
    tv_result.setText(String.valueOf(result));
}

通过这种方式,您将检查null或空的EditText,因此您的应用程序不会崩溃,如果您想要更加用户友好添加 Else 阻止并通过吐司 :)

快乐编码......

答案 3 :(得分:0)

试试这个,

EditText first_number = (EditText) findViewById(R.id.et_first);
EditText second_number = (EditText) findViewById(R.id.et_second);

String first = first_number.getText().toString();
String second = second_number.getText().toString();

if(!first.isEmpty() && !second.isEmpty()){
    float firstNumber = Float.parseFloat(first);
    float secondNumber = Float.parseFloat(second);
    TextView tv_result = (TextView) findViewById(R.id.tv_result);
    float result = firstNumber + secondNumber;
    tv_result.setText(String.valueOf(result));
}