2012-10-30 130 views
1

我想做一個簡單的計算器,倍數。我已經完成了大部分工作,但我一直得到相同的錯誤:運算符*未定義爲參數類型Double,EditText。運營商*是undefined

我搜索了其他人提出的問題,但唯一簡單的問題處理的不是雙打。有誰知道如何修理它?

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); 

    } 



} 
+1

需要說的是:擁有「一個」,「二個」和「三個」的「常量」比擁有神奇數字還要糟糕。它打開了像「最後一雙二十五」這樣搞砸的大門 - 如果那是你的意圖,那麼美德可能會憐憫你的靈魂。 – cHao

回答

6
EditText number = (EditText)findViewById(R.id.editText1); 


product = variable * number; 

numberEditText類型。您不能在EditText上申請*並加倍。

您的產品計算應該是:

product = variable * amount; 

amount是你EditText

+2

使用變量號來保存一個字符串令人困惑。 –

3

您已轉換的文本到一個名爲amount價值得到了價值,你卻已經忘記使用它。

product = variable * number; 

應該

product = variable * amount; 

這應該可以解決這個問題。

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 
+0

謝謝,我已經改變了,但它仍然無法正常工作。它說「局部變量,變量可能沒有被初始化。」 – Lucreacia

+0

@LucianCat checkmy編輯 – PermGenError

+0

感謝您的幫助! – Lucreacia