当的EditText为空,单击calculateButton,应用程序崩溃 [英] when EditText is empty and click calculateButton, app crashes

查看:103
本文介绍了当的EditText为空,单击calculateButton,应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2 EDITTEXT(weightT和heightT)......我的应用程序崩溃,如果我点击calculateButton ......是有一些方法来禁用calculateButton如果2 EDITTEXT是空的?或者用户没有输入EDITTEXT之一..或弹出信息请输入数字......类似的东西......请帮助...

下面是我的Java code:

 公共类BMIActivity延伸活动{

         @覆盖
     保护无效的onCreate(包savedInstanceState){
         super.onCreate(savedInstanceState);
         的setContentView(R.layout.activity_bmi);

       }

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.bmi,菜单);
    返回true;
}

公共无效calculateClickHandler(查看视图){



    如果(view.getId()== R.id.calculateButton){

                }

        EditText上weightText =(EditText上)findViewById(R.id.weightT);
        EditText上heightText =(EditText上)findViewById(R.id.heightT);
        TextView的resultText =(TextView中)findViewById(R.id.resultLabel);
        TextView的categoryText =(TextView中)findViewById(R.id.categoryLabel);
        RGROUP =(RadioGroup中)findViewById(R.id.rGroup);


    INT体重=(int)的Float.parseFloat(weightText.getText()的toString());
    INT身高=(int)的Float.parseFloat(heightText.getText()的toString());
    INT bmiValue = calculateBMI(体重,身高);

    字符串bmiInter pretation =跨pretBMI(bmiValue);

    resultText.setText(你的体重指数是:++ bmiValue ++和你);
    categoryText.setText(bmiInter pretation +。);}


        私人诠释calculateBMI(INT体重,身高INT){

    返程(INT)重* 703 /(身高×高);

        }
 

解决方案

在计算按钮preSS,检查这两个文本字段的内容。如果其中一个包含一个空字符串,你可以简单地返回打破了执行流程

 公共无效calculateClickHandler(查看视图){
    EditText上weightText =(EditText上)findViewById(R.id.weightT);
    EditText上heightText =(EditText上)findViewById(R.id.heightT);
    。字符串weightString = weightText.getText()的toString();
    。字符串heightString = heightText.getText()的toString();
    如果(view.getId()== R.id.calculateButton){
         如果(TextUtils.isEmpty(weightString)|| TextUtils.isEmpty(heightString)){
               //显示错误;
               返回;
         }
    }

     // ...等code ...
}
 

I have 2 editText (weightT and heightT)... my app crashes if I click the calculateButton... is there some way to disable the calculateButton if the 2 editText are empty?? or the user lack to input one of the editText .. or pop up message "Please input number"..something like that... Please Help...

Here is my java code:

  public class BMIActivity extends Activity {

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

       }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.bmi, menu);
    return true;
}

public void calculateClickHandler(View view) {



    if (view.getId() == R.id.calculateButton) {

                }

        EditText weightText = (EditText) findViewById(R.id.weightT);
        EditText heightText = (EditText)findViewById(R.id.heightT);
        TextView resultText = (TextView)findViewById(R.id.resultLabel);
        TextView categoryText = (TextView)findViewById(R.id.categoryLabel);
        rGroup  = (RadioGroup)findViewById(R.id.rGroup);


    int weight = (int) Float.parseFloat(weightText.getText().toString());
    int height = (int) Float.parseFloat(heightText.getText().toString());
    int bmiValue = calculateBMI(weight, height);

    String bmiInterpretation = interpretBMI(bmiValue);

    resultText.setText("Your BMI is:" + " " + bmiValue + " " + "and you're");
    categoryText.setText(bmiInterpretation + ".");}


        private int calculateBMI (int weight, int height) {

    return (int)  weight * 703 / (height * height) ;

        }

解决方案

When you press on the calculate button, check for the content of both the text fields. If one of them contains an empty string, you could simply return breaking the execution flow

public void calculateClickHandler(View view) {
    EditText weightText = (EditText) findViewById(R.id.weightT);
    EditText heightText = (EditText)findViewById(R.id.heightT);
    String weightString = weightText.getText().toString();
    String heightString = heightText.getText().toString();
    if (view.getId() == R.id.calculateButton) {
         if (TextUtils.isEmpty(weightString) || TextUtils.isEmpty(heightString)) {
               // show error; 
               return;
         }
    }

     //... other code ... 
}

这篇关于当的EditText为空,单击calculateButton,应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆