设置ListView时应用程序崩溃的原因是什么?

时间:2012-05-05 14:06:09

标签: android android-listview android-arrayadapter

我正在尝试运行一个例子,我在一本关于ListView的书上找到了它。正如本书所述,我在string-array文件中创建了string.xmlListView的布局位于list_item.xml文件夹内的res->layout

问题在于,当我运行活动时,应用程序崩溃了。谁能告诉我错误在哪里?

代码1

public class ListViewTestActivity extends ListActivity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] c = getResources().getStringArray(R.array.cities);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, c));
    }}

string.xml

<resources>

<string name="hello">Hello World, ListViewTestActivity!</string>
<string name="app_name">ListViewTest</string>

<string-array
        name="cities">
    <item>Bath</item>
    <item>Birmingham</item>
    <item>Bradford</item>
    <item>Brighton</item>
    <item>Bristol</item>
    <item>Cambridge</item>
    <item>Canterbury</item>
</string-array>

list_item.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="28dip">
    </TextView>


</LinearLayout>

logcat的

    05-05 22:45:16.018: E/ArrayAdapter(648): You must supply a resource ID for a TextView
05-05 22:45:16.048: E/AndroidRuntime(648): FATAL EXCEPTION: main
05-05 22:45:16.048: E/AndroidRuntime(648): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.AbsListView.obtainView(AbsListView.java:1430)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.ListView.makeAndAddView(ListView.java:1745)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.ListView.fillDown(ListView.java:670)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.ListView.fillFromTop(ListView.java:727)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.ListView.layoutChildren(ListView.java:1598)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.AbsListView.onLayout(AbsListView.java:1260)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.view.View.layout(View.java:7175)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.view.View.layout(View.java:7175)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.view.View.layout(View.java:7175)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.view.View.layout(View.java:7175)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.os.Looper.loop(Looper.java:130)
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-05 22:45:16.048: E/AndroidRuntime(648):  at java.lang.reflect.Method.invokeNative(Native Method)
05-05 22:45:16.048: E/AndroidRuntime(648):  at java.lang.reflect.Method.invoke(Method.java:507)
05-05 22:45:16.048: E/AndroidRuntime(648):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-05 22:45:16.048: E/AndroidRuntime(648):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-05 22:45:16.048: E/AndroidRuntime(648):  at dalvik.system.NativeStart.main(Native Method)
05-05 22:45:16.048: E/AndroidRuntime(648): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
05-05 22:45:16.048: E/AndroidRuntime(648):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)

4 个答案:

答案 0 :(得分:2)

如果对行项使用该特定布局,则将id分配给TextView并在适配器的构造函数中传递,以使适配器知道数据的放置位置:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/text"  
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="28dip">
</TextView>

并在您的活动中:

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, c));

答案 1 :(得分:0)

您的xml文件必须如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/monid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="28dip" />

答案 2 :(得分:0)

您可能正在list_item.xml关闭线性布局。 如果这不起作用,请发布日志

答案 3 :(得分:0)

您可能需要在onCreate中的某处调用setContentView(...)。