Android 当按下按钮且EditText为空时,我的应用程序不断崩溃

Android 当按下按钮且EditText为空时,我的应用程序不断崩溃,android,Android,我试过很多在这里找到的东西,但它总是崩溃。我想知道我需要添加什么才能在单击按钮且EditText为空时显示Toast消息 信息:应用程序应该将EditText中的两个值发送到另一个活动,在以下例外情况下显示Toast:第一个值大于6;第二个值大于(第一个值*14);如果字段为空(这是我的问题) 这是我的密码: public class MainActivity extends AppCompatActivity implements View.OnClickListener { Tex

我试过很多在这里找到的东西,但它总是崩溃。我想知道我需要添加什么才能在单击按钮且EditText为空时显示Toast消息

信息:应用程序应该将EditText中的两个值发送到另一个活动,在以下例外情况下显示Toast:第一个值大于6;第二个值大于(第一个值*14);如果字段为空(这是我的问题)

这是我的密码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView saludo_bienvenida;
    EditText et1_creditos;
    EditText et2_faltas;
    Button boton_ingresar;
    Button boton_info;
    String numero_creditos;
    String numero_faltas_actuales;




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

        saludo_bienvenida = (TextView) findViewById(R.id.saludo_bienvenida);

        et1_creditos = (EditText) findViewById(R.id.edittext1_numero_creditos);

        et2_faltas = (EditText) findViewById(R.id.edittext2_numero_faltas);

        boton_ingresar = (Button) findViewById(R.id.boton_ingresar);

        boton_info = (Button) findViewById(R.id.boton_info);

        if (boton_ingresar != null) {
            boton_ingresar.setOnClickListener(this);
        }

        if (boton_info != null) {
            boton_info.setOnClickListener(this);
        }

        et1_creditos.setInputType(InputType.TYPE_CLASS_NUMBER);
        et2_faltas.setInputType(InputType.TYPE_CLASS_NUMBER);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.boton_ingresar:
                numero_creditos = et1_creditos.getText().toString();
                numero_faltas_actuales = et2_faltas.getText().toString();
                int numero_creditos1 = Integer.parseInt(numero_creditos);
                int numero_faltas_actuales1 = Integer.parseInt(numero_faltas_actuales);
                int o = numero_creditos1 * 14;



                if (numero_creditos1>6) {
                    Toast.makeText(getApplicationContext(), "Ingrese un número válido de créditos", Toast.LENGTH_LONG).show();
                }else if (numero_faltas_actuales1 > o){
                    Toast.makeText(getApplicationContext(), "El número de faltas ingresadas es mayor que el número de horas del curso", Toast.LENGTH_LONG).show();
                }else{
                    Intent intent = new Intent(MainActivity.this,Resultados.class);
                    intent.putExtra("numero_creditos",numero_creditos);
                    intent.putExtra("numero_faltas_actuales",numero_faltas_actuales);
                    startActivity(intent);
                }
                break;
            case R.id.boton_info:
                Intent informacion = new Intent(MainActivity.this,Informacion.class);
                startActivity(informacion);
                break;
        }
    }
}
这是日志:

06-17 01:36:17.419 2738-2738/absence_counter.app.jorgepasco.com.absencecounter E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                             Process: absence_counter.app.jorgepasco.com.absencecounter, PID: 2738
                                                                                             java.lang.NumberFormatException: Invalid int: ""
                                                                                                 at java.lang.Integer.invalidInt(Integer.java:138)
                                                                                                 at java.lang.Integer.parseInt(Integer.java:358)
                                                                                                 at java.lang.Integer.parseInt(Integer.java:334)
                                                                                                 at absence_counter.app.jorgepasco.com.absencecounter.MainActivity.onClick(MainActivity.java:60)
                                                                                                 at android.view.View.performClick(View.java:4780)
                                                                                                 at android.view.View$PerformClick.run(View.java:19866)
                                                                                                 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:5254)
                                                                                                 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:903)
                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

请给我们看看你的日志箱

可能应用程序崩溃是因为您的交换机机箱中没有任何默认机箱。因此,如果v.getId()不是您所想的,它将崩溃

简单地说,在交换机机箱的末尾添加以下内容:

switch(v.getId()){
    case: ...
    case: ...
    default: Toast.makeText(getApplicationContext(),"v.getId = " + v.getId());
}
但我会更容易解决你的问题与你的日志框

et1\u creditos.getText()
可能会发送空值。那么null.toString()没有意义,因为null没有toString()

尝试:


谢谢你的日志

您可以在日志中看到,在解析int时遇到了问题。您尝试解析一个空字符串

因此,当用户不写任何东西时,您必须自己设置默认值。 您可以在解析之前执行类似操作:

if(numero_creditos.equals(""))
     numero_creditos = "0";

但是如果我是你,我会强迫用户直接右转一个整数,因为它在这一行失败了

        int numero_creditos1 = Integer.parseInt(numero_creditos);
如果您的numeor_creditos为空,则它将不是整数。 试一试


你看过航海日志了吗?是否找到给定的异常?可能一个NullPointerException没有你从EditText中得到的字符串,当然,你必须对“numero\u faltas\u actuales”做同样的事情,它起作用了!!!!非常感谢,真的。
        int numero_creditos1 = Integer.parseInt(numero_creditos);
int numero_creditos1;
if(TextUtils.isEmpty(numero_creditos){
    numero_creditos1 = 0;
} else {
    numero_creditos1 = Integer.parseInt(numero_creditos);
}