TextField为空时,应用程序崩溃 [英] App Crashes When TextField is Empty

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

问题描述

我尝试了太多事情,但是每次按下calculate(hesapla)按钮应用程序后都会崩溃。

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

例如:

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

}

}

有一个Log Cat;

There is a Log Cat;


com.ertugrulguleryeryuzgmail.sdunothesaplama E / AndroidRuntime:致命
例外:main
java.lang。 NumberFormatException:无效的int:在java.lang.Integer.invalidInt(Integer.java:138)
在java.lang.Integer.parseInt(Integer.java:359)
在java.lang.Integer.parseInt(Integer.java:332)
at
com.ertugrulguleryuzgmail.sdunothesaplama.MainActivity $ 1.onClick(MainActivity.java:74)
在android.view.View.performClick(View.java:4084)
在android.view.View $ PerformClick.run(View.java:16966)
在android.os.Handler.handleCallback(Handler .java:615)android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)机器人
app.ActivityThread.main(ActivityThread.java:4745)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)

com.android.internal os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
在dalvik.system.NativeStart.main(本机方法)

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)


推荐答案

那是因为要将空字符串转换为int。此处的

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

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

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