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 8820653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:39:41+00:00 2026-06-14T05:39:41+00:00

iv created an android app, what my problem is that when i press the

  • 0

iv created an android app, what my problem is that when i press the + = – / * buttons before i input a number my app stops working, is there anyway i can make it that if there is no number input it doesnt stop working?


package steven.mcilhone.calculatorcoursework;

import java.math.BigDecimal;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Calculator extends Activity {

EditText firstValue;
EditText secondValue;
TextView result;
Button addbtn, subtractbtn, dividebtn, multiplybtn, equalbtn, clearbtn;
BigDecimal firstNum, secondNum;



String Operator = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculator);

    addbtn = (Button) findViewById(R.id.addbtnID);
    subtractbtn = (Button) findViewById(R.id.subtractbtnID);
    dividebtn = (Button) findViewById(R.id.dividebtnID);
    multiplybtn = (Button) findViewById(R.id.multiplybtnID);
    equalbtn = (Button) findViewById(R.id.equalbtnID);
    clearbtn = (Button) findViewById(R.id.clearbtn);
    firstValue = (EditText) findViewById(R.id.edttxt1);
    secondValue = (EditText) findViewById(R.id.edttxt1);


    clearbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            EditText edttxt1 = (EditText) findViewById(R.id.edttxt1);
            edttxt1.setText("");
        }
    });



    addbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Operator = "+";
            addandclear();
        }
    });

    subtractbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Operator = "-";             
            addandclear();
        }
    });

    dividebtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Operator = "/";
            addandclear();

        }
    });

    multiplybtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Operator = "*";
            addandclear();
        }
    });

    equalbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            secondNum = new          BigDecimal(secondValue.getText().toString());
            EditText edttxt1 = (EditText) findViewById(R.id.edttxt1);
            edttxt1.setText("");
            if (Operator == "+"){
                edttxt1.setText(firstNum.add(secondNum).toString());
            }
            else if (Operator == "-"){
                edttxt1.setText(firstNum.subtract(secondNum).toString());
            }
            else if (Operator == "/"){
                edttxt1.setText(firstNum.divide(secondNum).toString());
            }   
            else if (Operator == "*"){
                edttxt1.setText(firstNum.multiply(secondNum).toString());
            }   



        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_calculator, menu);
    return true;
}


        public void addandclear(){


            firstNum = new BigDecimal(firstValue.getText().toString());             
            EditText edttxt1 = (EditText) findViewById(R.id.edttxt1);
            edttxt1.setText("");
        }



}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/edttxt1"
    android:layout_width="250dp"
    android:layout_height="100dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="?android:attr/editTextBackground"
    android:ems="15"
    android:inputType="numberDecimal" 
    android:gravity="right"/>

<Button
    android:id="@+id/addbtnID"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_below="@+id/edttxt1"
    android:layout_marginLeft="20dp"
    android:text="@string/add" />

<Button
    android:id="@+id/dividebtnID"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_below="@+id/edttxt1"
    android:layout_toRightOf="@+id/addbtnID"
    android:text="@string/divide" />

<Button
    android:id="@+id/multiplybtnID"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_below="@+id/edttxt1"
    android:layout_toRightOf="@+id/dividebtnID"
    android:text="@string/multiply" />

<Button
    android:id="@+id/subtractbtnID"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignBaseline="@+id/multiplybtnID"
    android:layout_alignBottom="@+id/multiplybtnID"
    android:layout_toRightOf="@+id/multiplybtnID"
    android:text="@string/subtract" />

<Button
    android:id="@+id/zerobtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignLeft="@+id/addbtnID"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/twobtn"
    android:text="@string/zero" />

<Button
    android:id="@+id/onebtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/zerobtn"
    android:layout_alignLeft="@+id/zerobtn"
    android:text="@string/one" />

<Button
    android:id="@+id/twobtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/zerobtn"
    android:layout_toLeftOf="@+id/multiplybtnID"
    android:text="@string/two" />

<Button
    android:id="@+id/threebtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/zerobtn"
    android:layout_toLeftOf="@+id/subtractbtnID"
    android:text="@string/three" />

<Button
    android:id="@+id/fourbtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/onebtn"
    android:layout_toLeftOf="@+id/twobtn"
    android:text="@string/four" />

<Button
    android:id="@+id/fivebtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignBaseline="@+id/fourbtn"
    android:layout_alignBottom="@+id/fourbtn"
    android:layout_toLeftOf="@+id/threebtn"
    android:text="@string/five" />

<Button
    android:id="@+id/sixbtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignBaseline="@+id/fivebtn"
    android:layout_alignBottom="@+id/fivebtn"
    android:layout_toRightOf="@+id/fivebtn"
    android:text="@string/six" />

<Button
    android:id="@+id/sevenbtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/fourbtn"
    android:layout_toLeftOf="@+id/fivebtn"
    android:text="@string/seven" />

<Button
    android:id="@+id/eightbtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/fivebtn"
    android:layout_toLeftOf="@+id/sixbtn"
    android:text="@string/eight" />

<Button
    android:id="@+id/ninebtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignBaseline="@+id/eightbtn"
    android:layout_alignBottom="@+id/eightbtn"
    android:layout_toRightOf="@+id/eightbtn"
    android:text="@string/nine" />

<Button
    android:id="@+id/equalbtnID"
    android:layout_width="70dp"
    android:layout_height="120dp"
    android:layout_alignBottom="@+id/threebtn"
    android:layout_alignTop="@+id/sixbtn"
    android:layout_toRightOf="@+id/threebtn"
    android:text="@string/equal" />

<Button
    android:id="@+id/clearbtn"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/equalbtnID"
    android:layout_alignLeft="@+id/equalbtnID"
    android:text="@string/clear" />

</RelativeLayout>
  • 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-14T05:39:43+00:00Added an answer on June 14, 2026 at 5:39 am

    In several places, you are converting the user input to a BigDecimal without doing any error checking. In particular, when there is no input, new BigDecimal(String) will generate a NumberFormatException because “” is not a valid number. From the docs:

    The string must contain at least one digit in either the integer or the fraction.

    Find every place where you are using the user input and put in some error checking. For instance, in addandclear():

    public void addandclear(){
        String val = firstValue.getText().toString();
        try {
            firstNum = new BigDecimal(val);
            EditText edttxt1 = (EditText) findViewById(R.id.edttxt1);
            edttxt1.setText("");
        } catch (NumberFormatException e) {
            if (val.length() == 0) {
                // blank field
            } else {
                // something wrong with the input
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem regarding Android App. I have created an application that download
Possible Duplicate: How to list my app downloads I created an android app that
I created a native Android app that basically hosts a webView control. That webView
I have an Android app created with Adobe AIR and Flex. How can I
Okay, so i am making an android app that has tabs, now my problem
So I've created an android app that plays video from blip.tv and various other
I've created a custom button in my Android app that has basically two different
I have created an Android app using the standard controls available. It seems that
I have a problem with my Android app. I have three Activities that all
I have an app that's fully working. It just has 1 problem with my

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.