2012-07-24 69 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

相关问题