tags:

views:

52

answers:

2

I'm trying to populate a list of files, and I'm using a custom ArrayAdapter so that each list item contains multiple Views. The code I use to populate the list is:

File home = MEDIA;
    int numFiles = home.listFiles().length;
    if(numFiles >0) {
        ArrayList<FileInfo> fileData = new ArrayList<FileInfo>(numFiles);

        for (File file : home.listFiles()) {
            FileInfo data = new FileInfo();
            data.title = file.getName();
            data.desc = file.getAbsolutePath();

            fileData.add(data);
        }

        FileAdapter fileAdapter = new FileAdapter(this, R.layout.file_item, fileData);
        setListAdapter(fileAdapter);

    }

The main XML file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>

    <TextView android:id="@+id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="No files found on SD Card."/>
</LinearLayout>

And the file_item XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView android:id="@+id/file_item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/file_item_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

Any time I run this and it tries to populate the list, it throws an exception and crashes the application, throwing the error

E/AndroidRuntime( 5320): FATAL EXCEPTION: main
E/AndroidRuntime( 5320): java.lang.RuntimeException: Binary XML file line #6: You must supply a layout_width attribute.
E/AndroidRuntime( 5320):    at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
E/AndroidRuntime( 5320):    at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3600)
E/AndroidRuntime( 5320):    at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3680)

If I add in an additional LinearLayout or something to the file_item XML file, the line that is missing the layout_width will change, which makes me think that it does believe that that first TextView in the file_item XML file is missing a layout_width attribute, but since there's one there already, I have no idea what it wants me to do.

A: 

Never had that, but one difference between your width and height is, that in file_item.xml you have twice "wrap_content" but for width you always have "match_parrent". Maybe it is solved if you give the outest layout a width like fill_parent.

WarrenFaith
match_parent = fill_parent, it was renamed in sdk version 8. If that is not the version you are working against, could cause problems though.
Mayra
match_parent replaced fill_parent in 2.2, so I don't think that can be the problem.
Paul
ah ok, thats what happens when you always have to work with api 4...
WarrenFaith
+1  A: 

I found the issue - this happens if you have a typo in the xmlns declaration.

I had:

xmlns:android="http://schemas.android/com/apk/res/android"

What I should have had was:

xmlns:android="http://schemas.android.com/apk/res/android"

For whatever reason this shows up in the stack trace as a missing layout_width.

Paul