1

我正在做一个简单的程序来添加从 EditText 输入的两个数字。但是,当用户单击“单击以添加”按钮将字段留空时,程序退出时说“不幸的是,程序已停止”。

这是代码:

public class MainActivity extends Activity {

long a, b, sum;
Button add, clr;
EditText e1, e2, res;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    add = (Button) findViewById(R.id.bAdd);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) throws NumberFormatException {
            // TODO Auto-generated method stub

                e1 = (EditText) findViewById(R.id.tv1);         
                String str1 = e1.getText().toString();

                e2 = (EditText) findViewById(R.id.tv2);
                String str2 = e2.getText().toString();

                try{
                    a = Integer.parseInt(str1);
                    b = Integer.parseInt(str2);
                }catch(NumberFormatException e){
                    res.setText("Please enter valid entries");
                }

                sum = a + b;    

                res = (EditText) findViewById(R.id.result);
                res.setText("Your sum is " + sum);
        }
    });

    clr = (Button) findViewById(R.id.bClr);
    clr.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) throws NumberFormatException {
            // TODO Auto-generated method stub
            e1.setText("");
            e2.setText("");
            res.setText("");
        }
    });
}

}

我希望应用程序为空白 EditText 条目响应“请输入有效条目”。

4

4 回答 4

1

因为您从编辑文本中获得了空文本..所以首先检查空

@Override
    public void onClick(View v) throws NumberFormatException {
        // TODO Auto-generated method stub

            e1 = (EditText) findViewById(R.id.tv1); 
            e2 = (EditText) findViewById(R.id.tv2);

            if( e1.getText() != null && e2.getText() != null) { 

            String str1 = e1.getText().toString();


            String str2 = e2.getText().toString();

            try{
                a = Integer.parseInt(str1);
                b = Integer.parseInt(str2);
            }catch(NumberFormatException e){
                res.setText("Please enter valid entries");
            }

            sum = a + b;    

            res = (EditText) findViewById(R.id.result);
            res.setText("Your sum is " + sum);
           }
    }
于 2012-12-13T09:14:51.427 回答
1

您的线路:

res = (EditText) findViewById(R.id.result);

必须移动,以便在使用之前初始化 res 变量res.setText。它没有在您的 catch 语句中初始化。将其移动到如下所示的位置。您可以在执行其他操作时对其进行初始化findViewById

e1 = (EditText) findViewById(R.id.tv1); 
e2 = (EditText) findViewById(R.id.tv2);
res = (EditText) findViewById(R.id.result);
于 2012-12-13T09:18:17.010 回答
0

是的。

添加这些行。

if (e1.getText().toString() == null || e2.getText().toString() == null)
{
       //Show dialog
}
else
{
    String str1 = e1.getText().toString();
    String str2 = e2.getText().toString();
    try{
                        a = Integer.parseInt(str1);
                        b = Integer.parseInt(str2);
                    }catch(NumberFormatException e){
                        res.setText("Please enter valid entries");
                    }

                    sum = a + b;    

                    res = (EditText) findViewById(R.id.result);
                    res.setText("Your sum is " + sum);
    }
}

以及有关警报对话框的任何帮助的教程

于 2012-12-13T09:15:17.363 回答
0

您可以在 try catch 块中捕获任何异常,而不是捕获 NumberFormatException 。请发布您收到的 Log Cat 错误。

于 2012-12-13T09:19:12.847 回答