2012-04-18 38 views
1

我敢肯定,這是死的簡單,因爲它是我的第一個帶有代碼的android應用程序(我所做的hello world示例只是給XML中的字符串賦值)。我的問題是當試圖獲得我的變量中的按鈕的引用,那麼在R.id中的id沒有定義?初學者Android爲什麼「id」是R.id的「Unknown member」?

編譯器錯誤是在下面的代碼在那裏出現一條註釋:

package com.geeksonhugs.simplecalc; 

import android.app.*; 
import android.os.*; 
import android.view.*; 
import android.widget.*; 

public class MainActivity extends Activity 
{ 
    private Button AddButton; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     AddButton = (Button)findViewById(R.id.btnAdd); 
//"Unknown member 'id' of 'com.geeksonhugs.simplecalc.R'" 

     //AddButton.setOnClickListener(this); 

    } 

} 

XML佈局文件:

<?xml version='1.0' encoding='utf-8' ?> 
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LayoutRoot" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical"> 

<TextView 
    android:id="@+id/txtFirstNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/strFirstNum" /> 

<EditText 
    android:id="@+id/edtFirstNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="" /> 

<TextView 
    android:id="@+id/txtSecondNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/strSecondNum" /> 

<EditText 
    android:id="@+id/edtSecondNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="" /> 

<Button 
    android:id="@+id/btnAdd" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/strAdd" 
    android:gravity="center" /> 

<TextView 
    android:id="@+id/txtResult" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="" /> 

</LinearLayout> 
+0

您是否使用軟件包或庫項目中的軟件包? – EboMike 2012-04-18 23:02:03

+0

請包括'MainActivity'的佈局XML文件。顯而易見的答案是,您在btnAdd ...中輸入了錯字 – RivieraKid 2012-04-18 23:03:47

+0

這是我的XML文件: – 2012-04-18 23:07:01

回答

1

R類代碼生成的是Android爲您構建工具,當你建立你的項目。這意味着有四種可能性:

  1. 您還沒有嘗試過構建項目,在這種情況下,嘗試一下。 Eclipse不認爲需要爲某種原因構建項目 - 從Eclipse菜單嘗試Project> Clean(當然,只有在使用Eclipse時才相關)。

  2. 您的清單中存在一些錯誤,或者您的某個資源阻止了R的構建。應該有Eclipse錯誤指示器。

  3. 您的佈局中沒有android:id屬性,因此沒有可用的R.id

+1

這顯然是我使用的IDE中的一個錯誤。我正在使用Android應用程序AIDE。我運行了一個選項來「刷新代碼分析」,並表示沒有錯誤。當我選擇「刷新生成」howerver時,它仍然報告錯誤(以及如果我試圖「運行」它)。 我的設備上有另一個名爲Terminal IDE的IDE(本質上是一個完整的Linux命令行環境),它編譯得很好。對不起,感謝您的幫助。下一次,我會在提出問題以消除IDE問題之前嘗試命令行IDE。 – 2012-04-18 23:31:44

1

請確保您粘貼的xml文件名爲main.xml並位於佈局文件夾下。 並嘗試再次生成R文件。 這可能有幫助。

+0

這顯然是我使用的IDE中的一個錯誤。我正在使用Android應用程序AIDE。我運行了一個選項來「刷新代碼分析」,並表示沒有錯誤。當我選擇「刷新生成」howerver時,它仍然報告錯誤(以及如果我試圖「運行」它)。 我的設備上有另一個名爲Terminal IDE的IDE(本質上是一個完整的Linux命令行環境),它編譯得很好。對不起,感謝您的幫助。下一次,我會在提出問題以消除IDE問題之前嘗試命令行IDE。 – 2012-04-18 23:32:26

+0

不用擔心,交配。談論IDE,我正在使用Intellij。我相信有一個Linux版本。如果你想,你可以試試。 – Luke 2012-04-19 23:10:36

+0

謝謝,我已經和Eclipse一起工作......作爲初學者,這只是查找示例並從社區獲得幫助的最佳方式。 – 2012-05-25 16:06:27

2

我解決了這個問題,通過刪除bin文件夾並使用AIDE App重新編譯。

1

對於那些使用AIDE時存在引用Id的問題,請嘗試刪除位於bin文件夾中的「gen文件夾」並重新編譯。問題是R.java類沒有創建一個名爲Id的構造函數。因此,刪除包含R.java類並重新編譯的gen文件夾可解決此問題。