1

I have tried too many things but after pressing calculate(hesapla) button app crashes everytime.

For example:

if (vize1.getText().toString().equals("") || fina1.getText().toString().equals("") || sort1.getText().toString().equals("")){
                    islem_yazi.setText("FILL THE FIELDS");
                } // IT DOESN'T WORK

What should I write/do?

    import android.app.ActionBar;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.text.TextUtils;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.Window;
    import android.view.WindowManager;
    import android.os.Bundle;

    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.google.android.gms.ads.AdRequest;

    import com.google.android.gms.ads.AdSize;
    import com.google.android.gms.ads.AdView;


public class MainActivity extends Activity {
AdView adView;

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

    reklamiYukle();
    {

    }

    final Button hsp_btn = (Button) findViewById(R.id.hesapla);
    Button temizle = (Button) findViewById(R.id.temiz1);
    final EditText vize1 = (EditText) findViewById(R.id.vize);
    final EditText fina1 = (EditText) findViewById(R.id.fina);
    final TextView islem_yazi = (TextView) findViewById(R.id.islem_yazi);
    final EditText sort1 = (EditText) findViewById(R.id.sort);

    hsp_btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            int v1 = Integer.parseInt(vize1.getText().toString());
            int f1 = Integer.parseInt(fina1.getText().toString());
            int s1 = Integer.parseInt(sort1.getText().toString());

            int m;
            float n, b, l, k, j, h;
            float bort;
            bort = (0.4f * v1) + (0.6f * f1);
            m = 1;
            n = 1.1f;
            b = 1.18f;
            l = 1.24f;
            k = 1.3f;
            j = 1.35f;
            h = 1.4f;

            if (vize1.getText().toString().equals("") || fina1.getText().toString().equals("") || sort1.getText().toString().equals("")){
                islem_yazi.setText("FILL THE FIELDS"); 
            }// IT DOES NOT WORK. APP CRASHES.

            else if (s1 > 100 || s1 < 0) {
                islem_yazi.setText("Sınıf Ortalaması 0 ile 100 Arası Olmalı");
            }
             else if (v1 > 100 || v1 < -2) {
                islem_yazi.setText("Vize Notunuz -2 ile 100 Arası Olmalı");
            }
             else if (f1 > 100) {
                islem_yazi.setText("Final/Büt Notunuz En Fazla 100 Olmalı");
            }
             else if (f1 < 0) {
                islem_yazi.setText("Final/Büt Sınavına Girmemişsiniz");
            }
             else if (s1 >= 60 && m * bort <= 38) {
                islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: FF ");


            } else if (s1 >= 60 && m * bort <= 45) {
                islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: FD");

            } else if (s1 >= 60 && m * bort <= 52) {
                islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: DD");


            } else if (s1 >= 60 && m * bort <= 59) {
                islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: DC");


            } else if (s1 >= 60 && m * bort <= 66) {
                islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: CC");


            } else if (s1 >= 60 && m * bort <= 73) {
                islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: CB");


            } else if (s1 >= 60 && m * bort <= 80) {
                islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: BB");


            } else if (s1 >= 60 && m * bort <= 87) {
                islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: BA");


            } else if (s1 >= 60 && m * bort <= 140) {
                islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: AA");

            }
        }
    });

    temizle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.temiz1);
            vize1.setText("");
            fina1.setText ("");
            sort1.setText("");
        }
    });

    }

private void reklamiYukle() {
    adView=new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(getString(R.string.reklam));
    LinearLayout layout= (LinearLayout) findViewById(R.id.reklam1);
    layout.addView(adView);

    AdRequest adRequest=new AdRequest.Builder().addTestDevice (AdRequest.DEVICE_ID_EMULATOR).build();
    adView.loadAd(adRequest);

}

}

There is a Log Cat;

com.ertugrulguleryuzgmail.sdunothesaplama E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NumberFormatException: Invalid int: "" at java.lang.Integer.invalidInt(Integer.java:138) at java.lang.Integer.parseInt(Integer.java:359) at java.lang.Integer.parseInt(Integer.java:332) at com.ertugrulguleryuzgmail.sdunothesaplama.MainActivity$1.onClick(MainActivity.java:74) at android.view.View.performClick(View.java:4084) at android.view.View$PerformClick.run(View.java:16966) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)

  • Print and post a stack trace. Can't tell why based on what you've posted. You can't either. I'll guess and say you'll find a null pointer exception b/c a variable isn't initialized properly. – duffymo Dec 14 '15 at 13:36
  • Show us the stacktrace...will help us a lot, to help you. Also do every one a favor and don't name objects and classes in other than english, in 99% cases its not an issue. – danizmax Dec 14 '15 at 13:37
  • I am a newbie on this and don't know what is stack trace. But I can show log cat; – Guleryuz Ertugrul Dec 14 '15 at 15:48

3 Answers3

0
vize1.getText().toString().equals("") 

change it to "".equals(vize1.getText().toString())

(apply this to rest of your conditions in if statement)


in your way if vize1 is null for any reason (maybe has not been declared yet) compiler will throw a nullPointException and crash the app.

bastami82
  • 5,955
  • 7
  • 33
  • 44
0

that's because you want to convert empty string to int. here:


 int v1 = Integer.parseInt(vize1.getText().toString());
        int f1 = Integer.parseInt(fina1.getText().toString());
        int s1 = Integer.parseInt(sort1.getText().toString());

Do this instead for all like:

 int v1 = "".equals(vize1.getText().toString()) ? 0 : Integer.parseInt(vize1.getText().toString());
bastami82
  • 5,955
  • 7
  • 33
  • 44
0

Use the isEmpty() method to test the condition. It uses less code and is more clear about your intentions.

72A12F4E
  • 1,744
  • 1
  • 14
  • 28