简体   繁体   中英

AlertDialog Force Close with EditText

I got the error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.crmdev.circuitorlcparalelo, PID: 6778
              java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
                  at com.crmdev.circuitorlcparalelo.MainActivity$3.onClick(MainActivity.java:70)
                  at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6626)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

For the following code with AlertDialog:

AlertDialog.Builder result = new AlertDialog.Builder(MainActivity.this);
        result.setTitle("Iterações");
        result.setMessage("Digite o número de iterações desejadas: ");
        final EditText iteracoes = new EditText(this);
        iteracoes.setInputType(InputType.TYPE_CLASS_NUMBER);
        iteracoes.setGravity(9);
        iteracoes.setPadding(10,10,10,10);
        result.setView(iteracoes);
        result.setPositiveButton("Calcular", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if(iteracoes.getText().equals("")){
                    Toast.makeText(getApplicationContext(), "Digite algum valor para as iterações!", Toast.LENGTH_LONG).show();
                    iteracoes.setText("");
                }
                else{
                    //Pega o numero de iterações
                    iteracao = Integer.getInteger(iteracoes.getText().toString());

                    tensaof = onCalculo(iteracao);

                    Bundle dad = new Bundle();
                    dad.putDouble("resultado", tensaof);
                    Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                    intent.putExtras(dad);
                    startActivity(intent);
                }
            }
        });
        result.show();

Every time i press the button Calcular the applicantion Force Close's, I change the function onCalculo but continues the same way. When the function onCalculo only altered a TextView , the application did not have this problem. I can't see what's going wrong. Thanks.

Because you are using wrong method to parse from a String to an Integer . You should you

iteracao = Integer.parseInt(iteracoes.getText().toString());

instead of

iteracao = Integer.getInteger(iteracoes.getText().toString());

By the way parseInt method might throws NumberFormatException so you should put in a try/catch block to prevent the app from crashing.

Integer iteracao = null;
try {
    iteracao = Integer.parseInt(iteracoes.getText().toString());
} catch (NumberFormatException e) {
    // TODO: Show a toast to let users know the input value is not valid
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM