我正在尝试制作一个倍数的简单计算器。我已经完成了大部分工作,但我仍然遇到同样的错误:对于参数类型Double,EditText,运算符*未定义。
我搜索了其他人提出的问题,但唯一简单的问题是处理不同于双打的问题。有谁知道如何修理它?
package com.deitel.multiplicationtables;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
//Implements the listener for an onclick event (implements View.onClickListener)
public abstract class Main extends Activity implements View.OnClickListener{
// creates a button
private Button one, two, three, four, five, six, seven, eight, nine;
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//assigns the resource id of 1 - 9 to each button.
one = (Button) findViewById(R.id.button1);
two = (Button) findViewById(R.id.button2);
three = (Button) findViewById(R.id.button3);
four = (Button) findViewById(R.id.button4);
five = (Button) findViewById(R.id.button5);
six = (Button) findViewById(R.id.button6);
seven = (Button) findViewById(R.id.button7);
eight = (Button) findViewById(R.id.button8);
nine = (Button) findViewById(R.id.button9);
//Adds the buttons to the onclicklistener
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
}
//creates a method (or action) for when the button is clicked.
public void onclick(View view)
{
//Makes a variable for the entered number
Double amount;
Double product;
Double variable;
// constants
final double one = 1;
final double two = 2;
final double three = 3;
if (view.getId() == R.id.button1)
{
variable = one;
}
if (view.getId() == R.id.button2)
{
variable = two;
}
if (view.getId()== R.id.button3)
{
variable = three;
}
//creates an editext and assigns the resource id of the xml edittext.
EditText number = (EditText)findViewById(R.id.editText1);
//Receives the input from the edittext, converts it to a double (number).
amount = Double.parseDouble(number.getText().toString());
//Calculates the product
product = variable * number;
//Creates a textview object, assigns the xml r.id, and then changes the text to report the amount.
TextView t = (TextView)findViewById(R.id.textView2);
t.setText("Your product is: " + product);
}
}
答案 0 :(得分:6)
EditText number = (EditText)findViewById(R.id.editText1);
product = variable * number;
number
的类型为EditText
。您无法在*
上申请EditText
并加倍。
您的产品计算应为:
product = variable * amount;
amount
是您从EditText
答案 1 :(得分:3)
您已将文字转换为名为amount
的值,但您忘记使用它。
该行
product = variable * number;
应该是
product = variable * amount;
这应解决问题。
答案 2 :(得分:3)
//creates an editext and assigns the resource id of the xml edittext.
EditText number = (EditText)findViewById(R.id.editText1);
//Calculates the product
product = variable * number;
您试图将double (variable)
与EditText (number)
类型相乘,因此Error
编辑您的本地变量错误:
local variables
没有得到default
值,在方法中使用它们之前必须initialize
public void onclick(View view)
{
//Makes a variable for the entered number
Double amount=0.0; //initialize this
Double product=0.0;//initialize this
Double variable=0.0;//initialize this