2011-06-13 28 views
-2

我在Eclipse(for Android)中做了這段代碼,但是有一些錯誤。字段無法解析?插入)和;?

佈局:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Click" 
    android:id="@+id/click" 
    /> 

<Edittext 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/meal" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/answerfield" 
    /> 

</LinearLayout> 

主營:

package tip.com.tip; 


import tip.com.R; 
import java.text.NumberFormat; 
import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class com extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final Button button = (Button) findViewById(R.id.click); 
     final EditText edittext = (EditText) findViewById(R.id.meal); 
     final TextView textView = (TextView) findViewById(R.id.answerfield); 

     button.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick (View v) { 
       String meal = edittext.getText().toString(); 
       String answer = ""; 

       if (meal.indexOf("$") == -1) { 
        meal = "$" + meal; 
       } 

       float fmp = 0.0F; 
       NumberFormat nf = java.text.NumberFormat.getCurrencyInstance(); 
       fmp = nf.parse(meal).floatValue(); 
       fmp *= 1.2; 

       answer = " The meal's price is: " + nf.format(fmp); 
       answerfield.setText(answer); 
      } 
     } 
} 
} 

它給我寫信,說anserfield不能被解決,語法錯誤,插入 ')',並在另一個錯誤插入' ;」。有什麼問題?我找不到這個錯誤。

+0

哪條線是錯誤? – slhck 2011-06-13 12:02:02

+0

setContentView(R.layout.main); final Button button =(Button)findViewById(R.id.click); final EditText edittext =(EditText)findViewById(R.id.meal); final TextView textView =(TextView)findViewById(R.id.answerfield); 這些是R無法解決的問題。 answerfield.setText(answer); - >它具有無法解決的問題 第二個'}'到底有沒有)和;問題。 – Zwiebel 2011-06-13 12:13:05

+0

看到我下面的答案,應該足以擺脫你的錯誤。 – slhck 2011-06-13 12:20:23

回答

3
  • 您的setOnClickListener缺少);,例如,最後一個右括號和分號。

  • 您的answerfield從未聲明,這要歸功於@WarrenFaith的提示。

我最初也曾暗示,試圖刪除以下行:

import tip.com.R; 

至少我記得有一次與它的問題。您可能需要檢查佈局XML文件中的語法錯誤。

+0

附加:變量'answerfield'永遠不會被聲明/初始化。目標TextView存儲在'textView'變量中。我不知道爲什麼導入應該被刪除,因爲它是用來獲取佈局和視圖... – WarrenFaith 2011-06-13 12:25:02

+0

我記得有這樣的問題一次。感謝您的提示,但更新的答案。 – slhck 2011-06-13 12:31:19

+0

謝謝。它解決了我的問題,除了),但現在沒問題。謝謝。 – Zwiebel 2011-06-13 12:48:59

0

看起來你缺少);從你的電話setOnClickListener