我是 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>