简体   繁体   中英

Cannot display Toast because of a runtime error

badd.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String s1 = e1.getText()+"hi".toString();
            Log.i("add",s1);
            if(e1.getText()==null)
            {
                Toast.makeText(getApplicationContext(), "Please Enter A Number First", Toast.LENGTH_SHORT).show();
            }
            else
            {
                num1 = Float.parseFloat(e1.getText()+"");
                i=1;
                e1.setText(null);
            }
        }
    });

I am making a simple calculator app, there is a Button badd(button to perform addition) and an EditText e1. when this button is clicked; first it'll be checked if the textbox is empty or not if it is empty it'll display a Toast telling user to enter a number. if the number is already entered that number will be stored in a variable num1 and textbox will get empty so that user can input another number and addition will be performed on the two numbers.

When i entered a number and pressed badd addition was done successfully. But my problem is that when i leave the edit text box empty (null) i get a runtime error and instead of displaying toast it shows an error "your application is unfortunately closed".

I created a string variable s1 also that takes the string entered in e1 and concate that string with another string "hi" so that if the string is empty it'll at least display a hi message in log cat. It is to check if there is some error in anonymous inner class. but it is cleared that there is no problem the anonymous inner class and else block.

I think the bug is in the if block. Please Help Me! Following are my LogCat errors that will help you to find the bug.

03-05 16:37:00.601: I/add(1569): hi

03-05 16:37:00.663: E/AndroidRuntime(1569): FATAL EXCEPTION: main

03-05 16:37:00.663: E/AndroidRuntime(1569): java.lang.NumberFormatException: Invalid float: ""

03-05 16:37:00.663: E/AndroidRuntime(1569):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at java.lang.StringToReal.parseFloat(StringToReal.java:289)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at java.lang.Float.parseFloat(Float.java:300)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at com.example.mycalculator.MainActivity$13.onClick(MainActivity.java:171)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at android.view.View.performClick(View.java:4202)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at android.view.View$PerformClick.run(View.java:17340)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at android.os.Handler.handleCallback(Handler.java:725)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at android.os.Handler.dispatchMessage(Handler.java:92)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at android.os.Looper.loop(Looper.java:137)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at android.app.ActivityThread.main(ActivityThread.java:5039)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at java.lang.reflect.Method.invokeNative(Native Method)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at java.lang.reflect.Method.invoke(Method.java:511)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

03-05 16:37:00.663: E/AndroidRuntime(1569):     at dalvik.system.NativeStart.main(Native Method)

According to the javadoc for parseFloat , it can throw NumberFormatException if the string can't be interpreted as a floating point number. So you should handle that case by catching that exception and doing something else instead:

try {
    num1 = Float.parseFloat(e1.getText()+"");
} catch (NumberFormatException e) {
    // do something else
}

try this for your if block:

if(e1.getText().toString().equals(""))

Hope this will help you..:)

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