【问题标题】:Activity Crashing When editText fields left empty当editText字段为空时活动崩溃
【发布时间】:2015-05-14 09:34:59
【问题描述】:

我已经看到了一些与我的类似的其他标题,但我的不同之处在于输入的字符串被解析为双精度,只要填充了所有 editText 字段,它就可以正常工作,但如果一个字段是留空它会回到最后一个活动!老实说,logcat 中的错误是模糊的,我认为它在谈论这样一个事实,即我已经实现的下一个活动的按钮没有意图,但它仍然崩溃,所以我不确定在哪里我错了。任何建议将不胜感激,这是我的代码和 logcat 错误:

package com.example.siuni.mymedicare;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class weighinscreen extends ActionBarActivity {

    private Button result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weighinscreen);

        result = (Button) findViewById(R.id.button1);

        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                EditText txtTempC = (EditText) findViewById(R.id.editText1);
                String strC = txtTempC.getText().toString();
                double TempC = Double.parseDouble(strC);//Parses the string as double

                EditText txtTempF = (EditText) findViewById(R.id.editText2);
                String strF = txtTempF.getText().toString();
                double TempF = Double.parseDouble(strF););//Parses the string as double


                EditText txtBPS = (EditText) findViewById(R.id.editText3);
                String strBPS = txtBPS.getText().toString();
                double BPS = Double.parseDouble(strBPS););//Parses the string as double


                EditText txtBPD = (EditText) findViewById(R.id.editText4);
                String strBPD = txtBPD.getText().toString();
                double BPD = Double.parseDouble(strBPD););//Parses the string as double


                EditText txtHeartR = (EditText) findViewById(R.id.editText5);
                String strH = txtHeartR.getText().toString();
                double HeartR = Double.parseDouble(strH););//Parses the string as double


                if (Double.compare(TempC, Double.NaN) == 0) {//Should check the double to see if it null and produce 

                    Toast.makeText(getApplicationContext(), "Please enter your temperature",
                            Toast.LENGTH_SHORT).show();
                    result.setEnabled(true);

                } else if (Double.compare(TempF, Double.NaN) == 0){//Should check the double to see if it null and produce the toast


                    Toast.makeText(getApplicationContext(), "Please enter your temperature",
                            Toast.LENGTH_SHORT).show();
                    result.setEnabled(true);
                } else if (Double.compare(BPS, Double.NaN) == 0) {//Should check the double to see if it null and produce the toast


                    Toast.makeText(getApplicationContext(), "Please enter your blood pressure",
                            Toast.LENGTH_LONG).show();
                    result.setEnabled(true);

                } else if (Double.compare(BPD, Double.NaN) == 0) {//Should check the double to see if it null and produce the toast


                    Toast.makeText(getApplicationContext(), "Please enter your blood pressure",
                            Toast.LENGTH_LONG).show();
                    result.setEnabled(true);

                } else if (Double.compare(HeartR, Double.NaN) == 0{//Should check the double to see if it null and produce the toast


                    Toast.makeText(getApplicationContext(), "Please enter your heart rate",
                            Toast.LENGTH_LONG).show();
                    result.setEnabled(true);

                } else if (TempC <=36 && TempF <= 99 && BPS <= 100 && BPD <= 40 && HeartR <= 60) {//If the doubles are equal to the figures produces the toast

                    Toast.makeText(getApplicationContext(), "Please enter contact your GP",
                            Toast.LENGTH_LONG).show();
                     startActivity(new Intent(weighinclass.this, register.class));//moves on the next actvity
                    result.setEnabled(false);

                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

还有错误:

05-14 08:57:56.944    2392-2407/com.example.siuni.mymedicare W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-14 08:57:56.944    2392-2407/com.example.siuni.mymedicare W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cc21c0, error=EGL_SUCCESS

【问题讨论】:

    标签: android android-intent android-activity android-logcat


    【解决方案1】:

    如果您在 EditText 中提供空值,应用程序将崩溃。

    您在 onClick 中获得 EditText 中的字符串(有时为空)并 试图解析它。

    它将在您的代码中 throw a NumberFormatException 并且您是 not using any Exception handling mechanism 。所以很明显你的应用程序会崩溃。

    提供一些检查,例如

    if(!strH.equals("")){    
    double HeartR = Double.parseDouble(strH););
    }
    else{
    Toast.makeText(getApplicationContext(), "Please enter your Heart Rate",Toast.LENGTH_LONG).show();
    }
    

    注意:提供的日志与您的问题无关。正确的日志会是这样的

    java.lang.NumberFormatException: Invalid double: ""
    

    【讨论】:

    • 我已经尝试按照您的建议实施这样的检查,但是使用该变量两次,否定了在代码中进一步使用该变量,它仍然崩溃但没有明显的错误。这是在模拟器和nexus 7上!
    • 请尝试获取 logcat 输出
    • 当我植入代码时,这是来自 logcat 的错误:java.lang.NumberFormatException: Invalid double: ""
    • 是的,正如我所料。您必须彻底重新检查您的验证部分。我敢肯定,您正在尝试在代码中的某处使用 Double.parse("")。
    • 我认为问题可能出在此处:EditText txtTempC = (EditText) findViewById(R.id.editText1);字符串 strC = txtTempC.getText().toString(); double TempC = Double.parseDouble(strC); 它声明了变量,但是当我在“if”语句中使用它时,它说它是多余的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多