final EditText number1 = (EditText) findViewById(R.id.editText1);
final EditText number2 = (EditText) findViewById(R.id.editText2);
final TextView result =(TextView) findViewById(R.id.textView3);
Button calculate = (Button) findViewById(new View.OnClickListener() {
我做错了什么?
public void onClick(View v) {
number1和数字要转换为double但我得到"不能在原始类型double"上调用getText()错误
double number1 = Double.parseDouble(number1.getText().toString());//error
double number2 = Double.parseDouble(number2.getText().toString());error
result.setText(String.valueOf(number1*number2));
答案 0 :(得分:1)
更改此
final EditText number1 = (EditText) findViewById(R.id.editText1);
final EditText number2 = (EditText) findViewById(R.id.editText2);
到
final EditText num1 = (EditText) findViewById(R.id.editText1);
final EditText num2 = (EditText) findViewById(R.id.editText2);
并使用
double number1 = Double.parseDouble(num1.getText().toString());
double number2 = Double.parseDouble(num2.getText().toString());
您有EditText numberr1
和double number1
使用MainActivity.this.varaiblename
引用在onClick
之外声明和初始化的变量。
你可以去查看
What is variable shadowing used for in a Java class?
找到更好的解释@
question about variable scope and shadowing in java
还有试试
try
{
final EditText num1 = (EditText) findViewById(R.id.editText1);
final EditText num2 = (EditText) findViewById(R.id.editText2);
}catch(NumberFormatException e){
e.printStacktrace();
}
如果字符串不包含可解析的double,则抛出NumberFormatException
。
答案 1 :(得分:1)
你不能拥有同名的变量/实例。因为您正在尝试创建具有相同名称的EditText
和double
的实例...这就是为什么它会给您错误。
现在,将您的变量名称double
更改为如下...
double number1 = Double.parseDouble(number1.getText().toString());
double number2 = Double.parseDouble(number2.getText().toString());
result.setText(String.valueOf(number1*number2));
于...
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
result.setText(String.valueOf(num1*num2));
答案 2 :(得分:1)
更改
double number1 = Double.parseDouble(number1.getText().toString());//error
double number2 = Double.parseDouble(number2.getText().toString());error
到
double number1 = Double.parseDouble(YourActivity.this.number1.getText().toString());//error
double number2 = Double.parseDouble(YourActivity.this.number2.getText().toString());error
您还需要制作两个EditText
个全局变量。您为EditText
和double
指定了相同的名称,因此在方法中创建的变量会影响EditText
答案 3 :(得分:0)
试试这个..
final EditText editText1_number1 = (EditText) findViewById(R.id.editText1);
final EditText editText2_number2 = (EditText) findViewById(R.id.editText2);
更改您的EditText
名称并尝试。
double number1 = Double.parseDouble(editText1_number1.getText().toString());
double number2 = Double.parseDouble(editText2_number2.getText().toString());
答案 4 :(得分:0)
使用
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
result.setText(String.valueOf(num1*num2));
而不是
double number1 = Double.parseDouble(number1.getText().toString());
double number2 = Double.parseDouble(number2.getText().toString());
result.setText(String.valueOf(number1*number2));
因为你已经
了final EditText number1 = (EditText) findViewById(R.id.editText1);
final EditText number2 = (EditText) findViewById(R.id.editText2);
你错误地使用了number1和number2 !!