簡體   English   中英

Android問題:嵌套線性布局/R.Java問題?

[英]Android Question: Nested Linear Layouts / R.Java Problem?

我在另一個論壇上讀到其他人有這個問題,他說他解決了這個問題,但是他沒有發布解決方案。 我的R.Java似乎沒有使用嵌套的LinearLayouts中的ID更新。 textViews中的前兩個ID確實出現在R.Java中(textView1和textView2),但是ImageButtons的后續ID都沒有...我懷疑這是因為布局(但是作為一個noobie我不知道為什么)。

我所擁有的布局幾乎是完美的,因此如果無法避免的話,我不希望重寫它。 但是我不知道如何讓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>

通常,嵌套布局和ID生成沒有問題。 但是,您缺少最外面的TableLayout的結束標記。 </TableLayout>添加到布局的末尾。 如果布局文件中有錯誤,Android將不會重新生成R文件-這就是為什么您看不到新ID的原因。

如果在Eclipse中使用XML編輯器,您會注意到一個紅色的“ x”圖標和錯誤消息:“ XML文檔結構必須在同一實體內開始和結束。” 如果轉到“問題”視圖(“ Window -> Show View -> Problems ),您也將看到此錯誤。

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

謝謝!!

一般的經驗法則是設置“自動構建”選項,以便在開始使用XML文件之前,R.java文件中的所有ID都會更新。

另外,如果您的xml文件中有任何錯誤,R.java也不會更新。 但是,SDK有時無法告訴您錯誤的確切位置。 相反,它只是不更新​​R.java。

請查看未使用UI元素ID更新的R.java文件,以獲取更多選項來解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM