加载自定义列表布局时,应用程序崩溃

时间:2011-10-06 18:46:49

标签: android

我正在尝试创建自定义列表布局。这是执行此操作的方法:

private void setUpList() {
    String[] items = {"item 1" , "item 2", "item 3","item 4" , "item 5", "item 6"};
    ArrayAdapter arrayadp = new ArrayAdapter(this, R.layout.list_layout, items);
    setListAdapter(arrayadp);       
}

这里是list_layout.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">
<TextView android:layout_width="match_parent"
          android:layout_height="match_parent" 
</LinearLayout>

如果我将R.layout.list_layout更改为android.R.layout.simple_list_item_1,它可以正常工作,但我需要使用自定义的。如何在不让应用程序崩溃的情况下执行此操作。

这是崩溃日志

10-06 17:59:55.488: ERROR/AndroidRuntime(6941): FATAL EXCEPTION: main
10-06 17:59:55.488: ERROR/AndroidRuntime(6941): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.srose.forumapp/com.srose.forumapp.HotelsActivity}: java.lang.NullPointerException
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.os.Looper.loop(Looper.java:132)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.app.ActivityThread.main(ActivityThread.java:4123)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at java.lang.reflect.Method.invoke(Method.java:491)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at dalvik.system.NativeStart.main(Native Method)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941): Caused by: java.lang.NullPointerException
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at com.srose.forumapp.HotelsActivity.setupListView(HotelsActivity.java:38)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at com.srose.forumapp.HotelsActivity.onCreate(HotelsActivity.java:29)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.app.Activity.performCreate(Activity.java:4397)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
10-06 17:59:55.488: ERROR/AndroidRuntime(6941):     ... 11 more

新的崩溃日志

10-06 19:11:08.648: ERROR/ArrayAdapter(8552): You must supply a resource ID for a TextView
10-06 19:11:08.648: DEBUG/AndroidRuntime(8552): Shutting down VM
10-06 19:11:08.648: WARN/dalvikvm(8552): threadid=1: thread exiting with uncaught exception (group=0x40014760)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552): FATAL EXCEPTION: main
10-06 19:11:08.678: ERROR/AndroidRuntime(8552): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:366)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.AbsListView.obtainView(AbsListView.java:1970)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1228)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.ListView.onMeasure(ListView.java:1139)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.View.measure(View.java:10828)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.View.measure(View.java:10828)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.View.measure(View.java:10828)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.View.measure(View.java:10828)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1889)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.View.measure(View.java:10828)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.ViewRoot.performTraversals(ViewRoot.java:938)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.view.ViewRoot.handleMessage(ViewRoot.java:2040)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.os.Looper.loop(Looper.java:132)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.app.ActivityThread.main(ActivityThread.java:4123)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at java.lang.reflect.Method.invoke(Method.java:491)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at dalvik.system.NativeStart.main(Native Method)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:383)
10-06 19:11:08.678: ERROR/AndroidRuntime(8552):     ... 32 more

2 个答案:

答案 0 :(得分:0)

尝试将此添加到textView

android:id="@android:id/text1"

你需要一个textView的id,simple_list_item_1有一个带有上面一行的textView。我认为这就是你所缺少的。

答案 1 :(得分:0)

除了添加android:id =“@ android:id / text1”之外,删除Layout标签,使其看起来像:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>