jai
jai

Reputation: 13

Multiplication between two text fields - Android Java

I am new to Android and programming and having a bit of difficulties with my code. I tried to research this but couldn't find out what was wrong, would appreciate the help. I am basically trying to multiply two values but every time I run my emulator and click on the button (calculate) it crashes:

Java Code:

 package com.example.cantcook;

 import java.math.BigDecimal;

 import android.app.Activity;
 import android.os.Bundle;
 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 costofmeal, amountofpeople;
    TextView costperperson;
    Button Calculate;
    BigDecimal costNum, percentNum;

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

        costofmeal = (EditText) findViewById(R.id.editText1); 
        amountofpeople = (EditText) findViewById(R.id.editText2);

        Calculate = (Button) findViewById(R.id.button1);

        costperperson = (TextView) findViewById(R.id.textView2);
        //costperperson.setText("£0.00");


        Calculate.setOnClickListener(new View.OnClickListener()
        {

        public void onClick (View v)
        {
            costNum = new BigDecimal(costofmeal.getText().toString());
            percentNum = new BigDecimal(amountofpeople.getText().toString());
            costperperson.setText(costNum.multiply(percentNum).toString());
        }
        });
    }

XML code:

<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"
    android:background="@drawable/background"
    tools:context=".Calculator" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="25dp"
        android:text="£"
        android:textAlignment="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="112dp"
        android:text="  Total spent on Shopping"
        android:textAlignment="center"
        android:textSize="18dp"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="  Amount of People"
        android:textAlignment="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="18dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="25dp"
        android:onClick="Btncalculate"
        android:text="Calculate" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="numberSigned" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="17dp"
        android:ems="10"
        android:inputType="numberDecimal"
        android:text="£" />

</RelativeLayout>

Upvotes: 1

Views: 2194

Answers (1)

Anton Cherkashyn
Anton Cherkashyn

Reputation: 5859

In your xml layout you have android:onClick="Btncalculate" in button1. What this does is when a click happens Android tries to look for Btncalculate method in your activity. And I'm assuming you don't have it there, since you're using an OnClickListener for button1(which, in my opinion, is better then using android:onClick).

Remove android:onClick="Btncalculate" from the xml file and it should work(unless there are other errors, in which case it would be great to see the stack trace).

Update

From your stacktrace:

java.lang.NumberFormatException: £5

Looks like you're code is trying to put "£5" into BigDecimal, which is not allowed, since it is not a number. To fix this you can either set android:inputType to a number in your editText1 and remove android:text="£", or use a separate method to filter out the £ sign from input.

Upvotes: 1

Related Questions