繁体   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