Sprawdzanie czy EditBox jest pusty

0

Witam Dopiero zaczynam bawic sie andoidem i mam nastepujacy problem.Pisze swoj kalkulator BMI i gdy zostawie puste pola aplikacja przestaje dzialac zrobilem do tego kod ale nie dziala wlasnie tylko to zostawianie pustych pol .Prosil bym o Pomoc z gory Dziekuje

public void Oblicz_BMI(View view) {
        double tempWeight;
        double tempHeight;
        double YourBMI;

        EditText Weight = (EditText) findViewById(R.id.WeightID);
        tempWeight = parseDouble(Weight.getText().toString());
        EditText Height = (EditText) findViewById(R.id.HeightID);
        tempHeight = parseDouble(Height.getText().toString());
        if (Weight.getText().toString().isEmpty())
        {
            Toast emptyWeight = Toast.makeText(MainActivity.this,"Weight TextField is Empty",Toast.LENGTH_SHORT);
            emptyWeight.show();
        }
        if (Height.getText().toString().isEmpty())
        {
            Toast emptyHeight = Toast.makeText(MainActivity.this,"Height TextField is Empty",Toast.LENGTH_SHORT);
            emptyHeight.show();
        }
        YourBMI = tempWeight / ((tempHeight*tempHeight)/10000);

        Intent intent = new Intent(MainActivity.this,Druga.class);
        intent.putExtra("BMI",YourBMI);
        startActivity(intent); 
0

Sprawdzasz jesli nie puste - wyswietla tosta jednak nie blokujesz przed wykonaniem obliczen.
Pisane z komorki

0

a Jak to zablokowac? :/

0

spróbuj tak:

if(Weight.getText().toString().equals(null) | Weight.getText().toString() == ""){
 // operacje
} else{
  Toast.makeText(context, "message", Toast.LENGTH...).show();
}
0

Niestety to samo

0

sorka, powinno być Weight.getText().toString().equals("") ;)

0

Nadal to samo :/

1
EditText Weight = (EditText) findViewById(R.id.WeightID);
String weightString = Weight.getText().toString();

if (weightString.length < 1 )
   Toast.makeText(getApplicationContext(), "EMPTY EDIT TEXT", Toast.LENGTH_SHORT).show();
1

Mieszasz poziomy abstrakcji w funkcji i dlatego nie widzisz bledu.
Zrob tak:

if(isEditTextEmpty(Weight)
    showError("Weight is empty")
else if(isEditTextEmpty(Height)
    showError("Height is empty")
else
   showBMI()
 
0

@panryz d3b8450da0.png

Kod:

  public void Oblicz_BMI(View view) {
        double tempWeight;
        double tempHeight;
        double YourBMI;
        String ifEmptyWeight,ifEmptyHeight;

        EditText Weight = (EditText) findViewById(R.id.WeightID);
        tempWeight =parseDouble(Weight.getText().toString());
        EditText Height = (EditText) findViewById(R.id.HeightID);
        tempHeight = parseDouble(Height.getText().toString());
        EditText checkWeight = (EditText) findViewById(R.id.WeightID);
        ifEmptyWeight=checkWeight.getText().toString();
        EditText checkHeight = (EditText)findViewById(R.id.HeightID);
        ifEmptyHeight=checkHeight.getText().toString();

        if(ifEmptyHeight.length()<1)
        {
            Toast emptyHeight = Toast.makeText(MainActivity.this,"Height TextBox is Empty",Toast.LENGTH_SHORT);
            emptyHeight.show();
        }
        else if(ifEmptyWeight.length()<1)
        {
            Toast emptyWeight = Toast.makeText(MainActivity.this,"Weight TextBox is Empty",Toast.LENGTH_SHORT);
            emptyWeight.show();
        }
        else {
            YourBMI = tempWeight / ((tempHeight * tempHeight) / 10000);

            Intent intent = new Intent(MainActivity.this, Druga.class);
            intent.putExtra("BMI", YourBMI);
            startActivity(intent);
            }

Logi:

06-24 08:18:53.256    1897-1897/com.example.miloszex.kalkulatorbmi E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.miloszex.kalkulatorbmi, PID: 1897
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NumberFormatException: Invalid double: ""
            at java.lang.StringToReal.invalidReal(StringToReal.java:63)
            at java.lang.StringToReal.parseDouble(StringToReal.java:267)
            at java.lang.Double.parseDouble(Double.java:301)
            at com.example.miloszex.kalkulatorbmi.MainActivity.Oblicz_BMI(MainActivity.java:55)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
1

No pewnie, zanim spradzasz czy jest pusto to robisz parse.
zrób parse dopiero jak jesteś pewny że coś tam jest

1 użytkowników online, w tym zalogowanych: 0, gości: 1