I'm trying to use a ListView inside of a RelativeLayout but when I run my app I get a runtimeexception with the message:
Binary XML file line #2: You must supply a layout_width attribute.
I tried putting layout_width attributes in every conceivable place in the xml resource files but so far no luck.
I attempt to populate the listview with this line of code:
.setListAdapter(new ArrayAdapter(this,
R.layout.tablerow3, R.id.label,
items));
Here's the tablerow3.xml contents:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="20dp"
android:layout_height="5dp"
android:id="@+id/tablerow01">
<Label android:id="@+id/label01"
android:layout_width="5dp"
android:layout_height="5dp"
android:textColor="@color/solid_white"
android:singleLine="true"/>
<Label android:id="@+id/label02"
android:layout_width="5dp"
android:layout_height="5dp"
android:textColor="@color/solid_white"
android:singleLine="true"/>
</LinearLayout>
Here's the xml开发者_开发问答 that contains the RelativeLayout(forex2.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Static Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="@+id/button_id">
</Button>
<Spinner android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_toRightOf="@id/button_id"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
/>
<ListView android:id="@android:id/list"
android:layout_width="5dp"
android:layout_height="5dp"
android:layout_alignParentBottom="true"
/>
<!-- android:layout_width="wrap_content"
android:layout_height="wrap_content" -->
</RelativeLayout>
The bug is here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="20dp"
android:layout_height="5dp"
android:id="@+id/tablerow01">
Look at the end of the line that starts with LinearLayout. You left a > which closes the tag, which means the attributes that follow are treated by the XML parser as a block of text, child of the LinearLayout tag. Just remove that extra > and you'll be fine.
With respect to your comment, there's no Android widget for a label. You probably want a TextView instead.
This is not an answer to your specific problem, but rather another cause of the same error.
I had a similar error, only my issue was that I entered schema, rather then schemas
ie.
LinearLayout xmlns:android="http://schema.android.com/apk/res/android" // wrong
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" // correct.
Hope this helps others.
精彩评论