jeudi 14 mai 2015

Activity Crashing When editText fields left empty

I've seen a few other headings similar to mine , but were mine differs is that the string entered is being parsed to a double and as long as all the editText fields are filled, it works fine, but if one field is left empty it goes back to the last activity! And if I'm honest the error is vague in logcat and I think it talking about the fact there is no intent on the button for the next activity, which I've implemented, but it still crashes, so I'm not sure where I'm going wrong. Any advice would be appreciated, here is my code and the logcat error:

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);
    }
}

And the error:

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

Aucun commentaire:

Enregistrer un commentaire