【问题标题】:My apps keeps crashing when button is pressed and EditText is empty [duplicate]按下按钮并且EditText为空时,我的应用程序不断崩溃[重复]
【发布时间】:2016-10-18 17:01:38
【问题描述】:

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

INFO:应用程序应该将 EditText 中的两个值发送到另一个活动,在以下异常中显示 Toast:第一个值高于 6;高于 (firstvalue*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)

【问题讨论】:

  • 读过logcat吗?发现给定的异常?也许是一个 nullPointerException
  • 你从 EditText contain a parsable integer987654321@得到的字符串是否

标签: android


【解决方案1】:

在这条线上失败了。

        int numero_creditos1 = Integer.parseInt(numero_creditos);

如果您的 numeor_creditos 为空,它将不是整数。 试试

int numero_creditos1;
if(TextUtils.isEmpty(numero_creditos){
    numero_creditos1 = 0;
} else {
    numero_creditos1 = Integer.parseInt(numero_creditos);
}

【讨论】:

    【解决方案2】:

    感谢您的日志!

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

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

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

    但如果我是你,我会强制用户直接右移一个整数

    【讨论】:

    • 当然,你必须对“numero_faltas_actuales”做同样的事情
    • 成功了!!!!非常感谢,真的。
    【解决方案3】:

    et1_creditos.getText() 可以发送一个空值。那么 null.toString() 没有意义,因为 null 没有 toString()。

    尝试:

    String creditos = et1_creditos.getText()
    if(creditos != null){
       //stuff here
    }
    

    【讨论】:

      【解决方案4】:

      请向我们展示您的日志。

      也许应用程序崩溃是因为您的 switch 案例中没有任何默认案例。所以如果 v.getId() 不是你想的那样,它会崩溃。

      只需将其添加到您的开关盒的末尾:

      switch(v.getId()){
          case: ...
          case: ...
          default: Toast.makeText(getApplicationContext(),"v.getId = " + v.getId());
      }
      

      但是我会更容易用你的日志来解决你的问题!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-29
        • 1970-01-01
        相关资源
        最近更新 更多