1

我是 Android 和编程新手,我的代码有点困难。我试图对此进行研究,但无法找出问题所在,不胜感激。我基本上是在尝试将两个值相乘,但是每次运行模拟器并单击按钮(计算)时,它都会崩溃:

Java 代码:

 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 代码:

<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>
4

1 回答 1

1

在您的 xml 布局中,您android:onClick="Btncalculate"在 button1 中。当点击发生时,Android 会尝试在您的活动中查找 Btncalculate 方法。而且我假设您没有它,因为您正在为 button1 使用 OnClickListener(在我看来,这比使用 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.

于 2013-03-01T00:29:16.817 回答