2012-07-24 66 views
0

我有一列與適配器一起工作的列,它工作正常。現在我試圖將它分成兩列,並且我收到了例外情況。Android - 嘗試顯示多列列表時出現異常

我下面這個教程,這似乎是最簡單的:http://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/

我有我宣佈這樣一個Java活動:

public class SeeAllQuestionsActivity extends ListActivity 

,這裏是一些代碼:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
    FlurryAgent.onStartSession(this, "8CA5LTZ5M73EG8R35SXG"); 

    setContentView(R.layout.all_question_page); 
... 

這裏是all_question_page xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="3px" 
     > 

<include android:id="@+id/header" 
     layout="@layout/header" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"/>  

<TextView 
    android:id="@+id/loading_questions" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Some prompt text." 
    android:textSize="10sp" 
    /> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="@+id/label" 
     android:textSize="20px" >   
    </ListView> 
</LinearLayout> 

順便說一句,如果有人能向我引用列表ID講解兩種語法差異性之探源,這將是巨大的:

android:id="@android:id/list" 
    android:id="@+id/list" 

僅供參考,這些都不是爲我工作:)

這裏是questions_list.xml佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="4dip" 
    android:paddingBottom="6dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView android:id="@+id/TRAIN_CELL" 
     android:layout_width="50dip" 
     android:layout_height="wrap_content"/> 

    <TextView android:id="@+id/FROM_CELL" 
     android:layout_width="70dip" 
     android:layout_height="wrap_content" android:layout_weight="1"/> 

    <TextView android:id="@+id/TO_CELL" 
     android:layout_width="60dip" 
     android:layout_height="wrap_content" android:layout_weight="1"/> 
</LinearLayout> 

當我在模擬器上運行它,它運行時異常抱怨這一行崩潰:setContentView(R.layout.all_question_page);和錯誤是:

java.lang.RuntimeException: 
Your content must have a ListView whose id attribute is 'android.R.id.list' 

請幫幫忙,我非常卡住

謝謝!!

+0

當您在代碼中設置列表時,您是否爲適配器和相應的R.id設置了整數數組。TRAIN_CELL,R.id.FROM_CELL和R.id.TO_CELL? – 2012-07-24 00:37:11

+0

@HowardHodson我在setContentView調用之前沒有設置它。我應該這樣做嗎?我沒有注意到這個教程。 – Genadinik 2012-07-24 00:39:10

回答

1

差異似乎是前者是你需要使用ListActivity,而後者是你會使用,如果你不打算使用ListActivity,而是隻是一個正常的活動。所以你必須堅持。 (注:我從來沒有親自使用ListActivity,所以我不能確切地證實這一點)

android:id="@android:id/list" 

我也建議您移除的ListView這些也行:

android:text="@+id/label" 
android:textSize="20px" 

無論是文本,也不是textSize是ListView的有效屬性。對於任何小部件,文本是一個有效的屬性,我不認爲你會想要設置文本等於"@+id/label"。如果你想從你的strings.xml文件中引用一個字符串,你需要使用"@string/label"。引用類似於文本的id會將一些十六進制代碼放入文本中,對用戶沒有任何意義(如果它工作的話)。

有可能消除那些可能會解決你的麻煩,如果是我的猜測是,其設置爲另一個ID文本令人困惑的東西,以爲您的列表沒有標識list

如果還是不解決你的問題我建議切換到一個簡單的活動,而不是ListActivity,並通過findViewById()獲得對你的ListView的引用,就像他們在你鏈接的例子中做的一樣。

+0

謝謝,現在嘗試它...有一件事是,當我擴展活動,而不是ListActivity,像這樣的ListView列表= getListView();開始給語法錯誤,因爲它們是ListActivity的一部分....並且我需要這個對象,因爲在後面的代碼中,我會執行這個lv.setOnItemClickListener(new OnItemClickListener(),它會在用戶按下其中一個列表項時監聽。 – Genadinik 2012-07-24 01:10:30

+0

當我拿出你提到的那兩行,我仍然得到相同的錯誤 – Genadinik 2012-07-24 01:14:33

+0

在實施你的建議後得到它的工作 - 謝謝你!:) – Genadinik 2012-07-24 01:34:21

相關問題