简体   繁体   中英

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. 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).

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. 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. You are however, missing an end tag for the outer-most TableLayout . Add a </TableLayout> to the end of your layout. 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.

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.' You would also be able to see this error if you went to the 'Problems' view ( Window -> Show View -> Problems ).

This ended up being an issue with Eclipse. I closed out the program and restarted it this morning, and everything was back updated in 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. It existed in my actual XML file but went MIA when I pasted it on the forum.

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.

Also, R.java doesn't get updated if there are any errors in your xml files. But, the SDK sometimes doesn't tell you where exactly the errors are. Instead, it just doesn't update R.java.

Check out R.java file not updating with UI element's id for more options to resolve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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