2011-09-04 62 views
-1

我在另一個論壇上看到別人有這個問題,他說他解決了,但他沒有發佈解決方案。我的R.Java似乎沒有用嵌套的LinearLayouts中的id進行更新。 textViews中的前兩個id確實出現在R.Java(textView1和textView2)中,但ImageButtons的後續id都沒有......我懷疑這是因爲佈局(但作爲noobie,我不知道爲什麼)。Android問題:嵌套線性佈局/ R.Java問題?

,因爲我有這幾乎是完美的,所以我討厭改寫它,如果我能避免它的佈局。但是我不知道如何讓R.java包含ID。而且我已經嘗試了通常的構建清理,刷新等。任何幫助都會受到我和我的電腦的極大讚賞......因爲如果我不能很快弄清楚,我最終可能會把它扔出窗外沮喪。 :)

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
> 

<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="86dp" android:background="@drawable/backgdtest" android:text=" Title of App" android:textColor="#ffffff" android:textSize="30px" android:textStyle="bold"/> 
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:text=" " android:layout_height="wrap_content"></TextView> 
    <TableRow android:layout_width="fill_parent" android:weightSum="3" android:layout_height="wrap_content"> 
     <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
      <ImageButton android:id="@+id/news" android:src="@drawable/news" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton> 
      <TextView android:layout_width="wrap_content" android:text=" Read News" android:layout_height="wrap_content"></TextView> 
     </LinearLayout> 
     <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
      <ImageButton android:id="@+id/messages" android:src="@drawable/message" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton> 
      <TextView android:layout_width="wrap_content" android:text=" Messages" android:layout_height="wrap_content"></TextView> 
     </LinearLayout> 
     <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
      <ImageButton android:id="@+id/phonebook" android:src="@drawable/phonebook" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton> 
      <TextView android:layout_width="wrap_content" android:text=" Phone Book" android:layout_height="wrap_content"></TextView> 
     </LinearLayout> 
    </TableRow> 
    <TableRow android:layout_width="fill_parent" android:weightSum="3" android:layout_height="wrap_content"> 
     <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
      <ImageButton android:id="@+id/planner" android:src="@drawable/planner" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton> 
      <TextView android:layout_width="wrap_content" android:text=" Day Planner" android:layout_height="wrap_content"></TextView> 
     </LinearLayout> 
     <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
      <ImageButton android:id="@+id/help" android:src="@drawable/panic" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton> 
      <TextView android:layout_width="wrap_content" android:text=" Call For Help" android:layout_height="wrap_content"></TextView> 
     </LinearLayout> 
     <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
      <ImageButton android:id="@+id/exit" android:src="@drawable/exit" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton> 
      <TextView android:layout_width="wrap_content" android:text=" Exit Program" android:layout_height="wrap_content"></TextView> 
     </LinearLayout> 
    </TableRow> 

回答

2

沒有問題(通常)具有嵌套佈局和ID的生成。但是,您錯過了最外面的TableLayout的結束標記。在佈局的末尾添加</TableLayout>。如果佈局文件中存在錯誤,Android將不會重新生成R文件 - 這就是您沒有看到新ID的原因。

如果您正在使用的XML編輯器,在Eclipse中,你會發現一個紅色的「X」圖標和錯誤信息:「XML文檔結構必須啓動並在同一實體內結束。」如果您轉到「問題」視圖(Window -> Show View -> Problems),您也將能夠看到此錯誤。

+0

實際上,實際的XML文件確實有標記,它只是沒有粘貼到原始問題中。但是我今天早上打開了Eclipse,注意到R.java中的所有內容都是正確的。所以我假設這只是Eclipse出於某種原因而沒有更新的問題。謝謝你! – AndroidNoobie

1

這最終成爲Eclipse的一個問題。今天早上我關閉了該程序並重新啓動了它,並且所有內容都在R.java中進行了更新。我沒有做任何其他改變。但請注意,正如其他答覆者指出的那樣,在我粘貼的XML末尾缺少一個標籤。它存在於我的實際XML文件中,但當我將其粘貼到論壇上時,它就進入了MIA。

謝謝!

+0

不是論壇!!一個! – Felix

0

一般的經驗法則是設置「自動構建」選項,這樣所有的ID在你的XML文件會在R.java文件在開始使用前更新。

此外,如果您的xml文件中有任何錯誤,R.java不會更新。但是,SDK有時不會告訴你錯誤的具體位置。相反,它只是不更新​​R.java。

退房R.java file not updating with UI element's id了更多選擇,以解決您的問題。