2010-10-05 45 views
2

我試圖填充文件的列表,並且我使用自定義ArrayAdapter,以便每個列表項包含多個視圖。我用它來填充列表的代碼是:當填充ListView,活動崩潰,由於缺少「layout_width」屬性

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

    } 

主要的XML文件是這樣的:

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

而且file_item XML文件看起來像這樣:

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

任何時間我運行這個,它試圖填充列表,它拋出一個異常並崩潰應用程序,拋出錯誤

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) 

如果我在file_item XML文件中添加額外的LinearLayout或其他內容,缺少layout_width的行將會更改,這讓我認爲它確實認爲file_item XML文件中的第一個TextView缺少layout_width屬性,但既然已經有一個,我不知道它想要我做什麼。

回答

4

我發現了這個問題 - 如果您在xmlns聲明中有錯字,會發生這種情況。

我:

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

我應該有是:

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

無論出於何種原因,這顯示了在堆棧跟蹤缺少layout_width。

0

從未有過這一點,但你的寬度和高度之間的一個區別是,在file_item.xml你有兩次「WRAP_CONTENT」但寬度你總是有「match_parrent」。也許這是解決,如果你給最後的佈局寬度像fill_parent

+0

match_parent = FILL_PARENT,它的SDK版本8改名如果不是您正在處理的版本可能會導致問題。 – 2010-10-05 22:33:49

+0

match_parent在2.2中替換了fill_parent,所以我不認爲這可能是問題所在。 – Paul 2010-10-05 22:40:19

+0

嗯,那就是當你總是需要使用api 4時會發生什麼...... – WarrenFaith 2010-10-05 23:10:16

0

我有一個類似的神祕錯誤,在我的XML文件中沒有任何拼寫錯誤。我解決了它最終被替換

android:layout_width="match_parent" 
android:layout_height="match_parent" 

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

這是我的LinearLayout標籤自動生成