2012-04-16 77 views
0

我對列表視圖項目使用以下佈局。ListView的項目佈局錯誤

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://scheme.android.com/apk/res/android" 
    android:id="@+id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:paddingLeft="10dip" 
    android:textSize="18sp" 
    android:minHeight="40sp" 
    /> 

但是我在logcat java.lang.RuntimeException中得到一個錯誤...您必須提供一個layout_width參數。

我的main.xml是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/widget28" 
    android:background="#ff000033" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:layout_width="70px" 
     android:layout_height="30px" 
     android:paddingLeft="2px" 
     android:paddingTop="2px" 
     android:background="@drawable/ic_launcher" 
    ></ImageView> 

    <ListView 
     android:id="@+id/myListView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 

,這是我活動的onCreate代碼

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    context=this.getApplicationContext(); 
    this.setTitle("NP Headline News"); 

    myListView=(ListView)findViewById(R.id.myListView); 
    myListView.setOnItemClickListener(new OnItemClickListener(){ 

      @Override 
     public void onItemClick(AdapterView<?> arg0, View v, int index,long id) { 
       // TODO Auto-generated method stub 
       String urlAddress=urlAddresses[index]; 
       String urlCaption = urlsCaptions[index]; 
      } 

    }); 

    int layoutId=R.layout.simple_list_item_1; 
    // int layoutId=android.R.layout.simple_list_item_1; 
    aa = new ArrayAdapter<String>(this,layoutId,this.urlsCaptions); 

    myListView.setAdapter(aa); 
} 

我認爲,這是因爲如果我用android.R.layout與我simple_list_item_1管理的東西。 simple_list_item_1然後每件事情都可以正常工作

+0

你可以發佈完整的錯誤文本 – 2012-04-16 13:51:41

+0

你確定RuntimeException引用這個佈局嗎?這似乎是正確的...請粘貼列表視圖佈局。 – 2012-04-16 13:51:49

回答

2

這是你的錯誤清除佈局:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:paddingLeft="10dip" 
    android:textSize="18sp" 
    android:minHeight="40sp" 

    /> 

你能能夠區分您的錯誤包含的代碼,這上面一。

我希望大家不要緊張,而要求代碼的東西,那一定是導致你堵塞和困惑的頭腦。

對於這些類型的錯誤,您可以使用DDMS的幫助來調試自己。 因爲代碼是非常非常簡單的查看權限。

約在以前的答案有些話:

傢伙,是不是需要聲明TextView或任何View只內Layout文件。

這也是一個View,所以它可以獨自坐在Canvas

而且問題不在"fill_parent""match_parent"東西。

我想你現在可能也會緊張,因爲我花了這麼多時間來給你錯誤的原因。

在這裏,我們走了,

錯誤是因爲只需在下面一行,

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

在上述行,你誤輸入scheme而不是schemas

這是錯誤。

所以,只有我提到過,如果你已經用外面的時間來看代碼,你可以很容易地得到它。

xmlns表示命名空間,這應該是正確的,以使android:前綴屬性有價值。

希望你明白,答案對你有幫助。

+0

謝謝。對不起,錯別字 – MARK 2012-04-16 14:27:08

+0

@Raza沒有probs的傢伙。寒意......做得好 – 2012-04-16 14:32:13

+0

「體內增加了910個字符」 - 真的嗎?你不能找到更好的描述嗎?討論[this](http://stackoverflow.com/suggested-edits/241913)並使用這種聯繫方式,因爲我們沒有私人消息系統。無論如何 - 建議編輯時請妥善處理。 – 2012-04-17 12:02:22

0

您應該在佈局下觸摸您的TextView,因爲您使用的是獨特的TextView,您可以添加一個LinearLayout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 
<TextView xmlns:android="http://scheme.android.com/apk/res/android" 
    android:id="@+id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:paddingLeft="10dip" 
    android:textSize="18sp" 
    android:minHeight="40sp" 
    /> 
</LinearLayout> 
+1

我認爲可以添加一個Textview。是否有意義是另一個問題,尤其是某些屬性可能是無用的 – 2012-04-16 14:02:33

+0

@Pedro Teran:將'TextView'放入'Layout'中並不是強制性的。我們可以像他一樣使用它。 – 2012-04-16 14:08:19

0

我想問題來自於不同的東西。

您正在執行自定義項目視圖,但您使用的是標準AdapterView。

對於標準arrayAdapter,您需要完成預期的結構。

或者你打造一個自己的ArrayAdapter和供應竭誠爲您getView

Android Custom Listview