简体   繁体   English

Android问题:嵌套线性布局/R.Java问题?

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

I read at another forum that someone else had this problem, he said he resolved it, but he didn't post the solution. 我在另一个论坛上读到其他人有这个问题,他说他解决了这个问题,但是他没有发布解决方案。 My R.Java doesn't seem to be updating with the id's in the nested LinearLayouts. 我的R.Java似乎没有使用嵌套的LinearLayouts中的ID更新。 The first two id's in the textViews do appear in the R.Java (textView1 and textView2), but none of the subsequent id's for the ImageButtons do...and I suspect it's because of the layout (but being a noobie I have no idea why). textViews中的前两个ID确实出现在R.Java中(textView1和textView2),但是ImageButtons的后续ID都没有...我怀疑这是因为布局(但是作为一个noobie我不知道为什么)。

The layout as I have it is almost perfect, so I'd hate to rewrite it if I can avoid it. 我所拥有的布局几乎是完美的,因此如果无法避免的话,我不希望重写它。 But I don't know how to get the R.java to include the ID's. 但是我不知道如何让R.java包含ID。 And I've tried the usual build clean, refresh, etc. Any help would be greatly appreciated by me and my computer...because if I can't figure this out soon, I'll probably end up throwing it out the window in frustration. 而且我已经尝试了常规的构建清理,刷新等操作。我和我的计算机将非常感谢您的任何帮助...因为如果我很快无法解决这个问题,我可能最终会将其扔到窗外感到沮丧 :) :)

<?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>

There is no problem (in general) with nested layouts and ID generation. 通常,嵌套布局和ID生成没有问题。 You are however, missing an end tag for the outer-most TableLayout . 但是,您缺少最外面的TableLayout的结束标记。 Add a </TableLayout> to the end of your layout. </TableLayout>添加到布局的末尾。 Android won't regenerate the R file if there are errors in a layout file - this is why you aren't seeing the new IDs. 如果布局文件中有错误,Android将不会重新生成R文件-这就是为什么您看不到新ID的原因。

If you are using the XML editor in Eclipse you will notice a red 'x' icon and the error message: 'XML document structures must start and end within the same entity.' 如果在Eclipse中使用XML编辑器,您会注意到一个红色的“ x”图标和错误消息:“ XML文档结构必须在同一实体内开始和结束。” You would also be able to see this error if you went to the 'Problems' view ( Window -> Show View -> Problems ). 如果转到“问题”视图(“ Window -> Show View -> Problems ),您也将看到此错误。

This ended up being an issue with Eclipse. 这最终成为Eclipse的问题。 I closed out the program and restarted it this morning, and everything was back updated in R.java. 我关闭了该程序,并于今天早晨重新启动了该程序,所有内容都在R.java中重新更新。 I made no other changes. 我没有做其他更改。 Note though, as the other answerer pointed out, a tag was missing at the end of my XML that I pasted. 但是请注意,正如另一个回答者指出的那样,我粘贴的XML末尾缺少一个标签。 It existed in my actual XML file but went MIA when I pasted it on the forum. 它存在于我的实际XML文件中,但是当我将其粘贴到论坛上时就变为MIA。

Thanks!! 谢谢!!

A general rule of thumb is to set the 'Build Automatically' option so that all the id's in your xml files get updated in the R.java file before you start using them. 一般的经验法则是设置“自动构建”选项,以便在开始使用XML文件之前,R.java文件中的所有ID都会更新。

Also, R.java doesn't get updated if there are any errors in your xml files. 另外,如果您的xml文件中有任何错误,R.java也不会更新。 But, the SDK sometimes doesn't tell you where exactly the errors are. 但是,SDK有时无法告诉您错误的确切位置。 Instead, it just doesn't update R.java. 相反,它只是不更新​​R.java。

Check out R.java file not updating with UI element's id for more options to resolve your problem. 请查看未使用UI元素ID更新的R.java文件,以获取更多选项来解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM