我想創建一個自定義列表佈局。下面是做它的方法:應用程序在加載自定義列表佈局時崩潰
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
後崩潰日誌? –
嘗試添加到您的textView android:id =「@ android:id/text1」,你需要一個id爲textView。 –
嗨,試過,它仍然崩潰,我會把崩潰日誌放在原來的帖子 – sam