当我单击按钮而不提供输入时,应用程序立即关闭

时间:2015-08-27 17:24:32

标签: android

unfortunatly this app has closed

当我按下任何输入的按钮时,会看到这种情况。我有什么方法可以阅读价值观。需要一些建议来改进我的代码。还有我的xml文件。顺便说一下,我是初学者。所以请帮助我,以便提高我的编码技能。请继续这么长的帖子。enter image description here

package com.example.simplecalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


  public class MainActivity extends Activity {
  Button add,subtract,multiply,divide;
  EditText firstValue,secondValue;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    firstValue=(EditText) findViewById(R.id.editText1);
    secondValue=(EditText) findViewById(R.id.editText2);
    add=(Button)findViewById(R.id.add);

    add.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            final int val1 =    Integer.parseInt(firstValue.getText().toString() );
            final int val2 = Integer.parseInt(secondValue.getText().toString() );
            TextView output= (TextView) findViewById(R.id.answer);
            output.setText(" "+(val1+val2));

}
});
subtract=(Button)findViewById(R.id.subtract);

subtract.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
    final int val1 = Integer.parseInt( firstValue.getText().toString() );
    final int val2 = Integer.parseInt(secondValue.getText().toString() );
    TextView output= (TextView) findViewById(R.id.answer);
    output.setText(" "+(val1-val2));

}
});
multiply=(Button) findViewById(R.id.multiply);
multiply.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
final float val1=Integer.parseInt(firstValue.getText().toString());
final float val2=Integer.parseInt(secondValue.getText().toString());
TextView output=(TextView) findViewById(R.id.answer);
output.setText(""+(val1*val2));
}
});
divide=(Button) findViewById(R.id.divide);
divide.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
final float val1=Integer.parseInt(firstValue.getText().toString());
final float val2=Integer.parseInt(secondValue.getText().toString());
TextView output=(TextView) findViewById(R.id.answer);
output.setText(""+(val1/val2));
        }
    });
}
}

3 个答案:

答案 0 :(得分:2)

您正在尝试从空字符串中解析整数。将您的onClick方法修改为以下内容:

public void onClick(View v){
    if(firstValue.getText().toString().isEmpty() || 
            secondValue.getText().toString().isEmpty())
        return;

    final int val1 = Integer.parseInt(firstValue.getText().toString() );
    final int val2 = Integer.parseInt(secondValue.getText().toString() );
    TextView output= (TextView) findViewById(R.id.answer);
    output.setText(" "+(val1-val2));
}

答案 1 :(得分:2)

我认为你的 logcat 会抛出Nullpointer Exception。 因此,当您单击按钮而未进行适当的验证时,它会崩溃。

所以你需要添加适当的验证。使用 isEmpty()来避免这个问题

答案 2 :(得分:1)

在执行任何操作之前检查EditTexts是否为空。您可以按照以下方式执行此操作

if(!firstValue.getText().toString().matches(""))
{
     //Enters if not empty
     //Perform operations here
}

例如。您的add.setOnClickListener()应该是

add.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            if(!firstValue.getText().toString().matches("") && !secondValue.getText().toString().matches(""))
            {
                 final int val1 =    Integer.parseInt(firstValue.getText().toString() );
                 final int val2 = Integer.parseInt(secondValue.getText().toString() );
                 TextView output= (TextView) findViewById(R.id.answer);
                 output.setText(" "+(val1+val2));
            }
        }
});