Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8188925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:04:47+00:00 2026-06-07T03:04:47+00:00

I have an Activity with an EditText, Button and TextView. If I write some

  • 0

I have an Activity with an EditText, Button and TextView. If I write some numbers and click the button, the TextView shows the result of the calculation.

If the EditText is empty (i.e. there are no numbers entered) and I click the Button I have the a dialog saying: “The application has stopped unexpectedly. Please try again”.

Please help me. I want to show a window with some comments (for example “Insert numbers”), if the user does not write any numbers. How should I program the EditText field?
How to solve this? Thanks a lot

This is my onClick method:

public void onClick(View v) {
    EditText numA  = (EditText)findViewById(R.id.editText1);
    TextView wynik1 = (TextView)findViewById(R.id.wynik);

    float num1 = Float.parseFloat(numA.getText().toString());

    float eq1 = 0;

    if(num1>0){ 
        switch (v.getId()) {
                case R.id.oblicz:
                    eq1 = 2*num1;
                break;
        }

        wynik1.setText(String.format("%f", eq1));   
    }
    else {
        Intent o = new Intent(this, Obliczokno.class);
        startActivity(o);
    }
}

I have changed onClick method:

public void onClick(View v) {
    EditText numA  = (EditText)findViewById(R.id.editText1);

    float num2 = Float.parseFloat(numA.getText().toString());
    float eq1 = 0;
    float eq2 = 0;  

    try{  float num1 = Float.parseFloat(numA.getText().toString());

            if(num1>0 || num2>0){   

                switch (v.getId()) {
                case R.id.pole:
                    eq1 = 2*num1 ;

                break;  

                case R.id.obwod:
                    eq2 = 3*num1 ;

                break;  

                }}

                else {Intent o = new Intent(this, Obliczokno.class);
                startActivity(o);}
        }
         catch (NumberFormatException e) {
           Toast.makeText(this, "Sorry you did't type anything", Toast.LENGTH_SHORT).show();

           return;
        }

and I have errors. My LogCat errors:

E/AndroidRuntime(321): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(321): java.lang.NumberFormatException:
E/AndroidRuntime(321):  at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:296)
E/AndroidRuntime(321):  at java.lang.Float.parseFloat(Float.java:327)

E/AndroidRuntime(321):  at arek.geometria.Oblicz.onClick(Oblicz.java:35)

E/AndroidRuntime(321):  at android.view.View.performClick(View.java:2344)

E/AndroidRuntime(321):  at android.view.View.onTouchEvent(View.java:4133)

E/AndroidRuntime(321):  at android.widget.TextView.onTouchEvent(TextView.java:6510)

E/AndroidRuntime(321):  at android.view.View.dispatchTouchEvent(View.java:3672)

E/AndroidRuntime(321):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

E/AndroidRuntime(321):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

E/AndroidRuntime(321):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

E/AndroidRuntime(321):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

E/AndroidRuntime(321):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)

E/AndroidRuntime(321):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)

E/AndroidRuntime(321):  at android.app.Activity.dispatchTouchEvent(Activity.java:1987)

E/AndroidRuntime(321):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)

E/AndroidRuntime(321):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)

E/AndroidRuntime(321):  at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(321):  at android.os.Looper.loop(Looper.java:123)

E/AndroidRuntime(321):  at android.app.ActivityThread.main(ActivityThread.java:4203)

E/AndroidRuntime(321):  at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(321):  at java.lang.reflect.Method.invoke(Method.java:521)

E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)

E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)

E/AndroidRuntime(321):  at dalvik.system.NativeStart.main(Native Method)

The if … else works correctly.
Have you got any ideas? Please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:04:48+00:00Added an answer on June 7, 2026 at 3:04 am

    You just need to do a check on what the user has typed:

    String input = editText.getText().toString();
    
    if(input == null || input.trim().equals("")){
          Toast.makeText(context, "Sorry you did't type anything"), Toast.LENGTH_SHORT).show();
    }
    

    EDIT

    If you have a float, you have to do the check before you parse the float:

    String input = editText.getText().toString();
    
    if(input == null || input.trim().equals("")){
          // Toast message
          return;
    }
    float num1 = Float.parseFloat(input);
    

    or

    try{
    
       float num1 = Float.parseFloat(numA.getText().toString());
    
    } catch (NumberFormatException e) {
       // Toast message - you cannot get a float from that input
       return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity that shows some TextView s and EditText s, as well
I have implemented activity with edittext and button: After user entered a password and
So i have this test Activity with 3 elements: TextView, EditText, Button. When user
friends, i have a EditText on simple activity with a button. when every i
I have a EditText , a textView and a button, I need to set
I have an activity that contains several user editable items (an EditText field, RatingBar,
I have an Activity in Android, with two elements: EditText ListView When my Activity
I have a layout which contains some views like this: <LinearLayout> <TextView...> <TextView...> <ImageView
I have a form which renders, but when I click the submit button, I
I have a relatively simple layout that contains an EditText . The activity itself

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.