我有一个包含两个列表的布局。由于您不能将列表放入 a 中,因此ScrollView
我正在尝试根据此问题ExpandableListView
使用带有自定义的.
列表布局:SimpleExpandableListAdapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ExpandableListView android:id="@+id/android:list"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
现在,当我尝试添加一些内容作为标题时,麻烦就来了。如果我从代码创建布局,它会起作用,如下所示:
LinearLayout ll = new LinearLayout( this );
ll.setOrientation( LinearLayout.VERTICAL );
TextView t = new TextView( this );
t.setText( "some text" );
ll.addView( t );
ImageView i = new ImageView( this );
i.setImageResource( R.drawable.icon );
ll.addView( i );
getExpandableListView( ).addHeaderView( ll );
如果我使用相同的布局,或者甚至比代码中的布局更简单,应用程序甚至在屏幕上显示之前就崩溃了。这是我想作为标题添加到上述列表的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:id="@+id/ll">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="some text"/>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/icon"/>
</LinearLayout>
和代码:
LinearLayout ll = (LinearLayout)findViewById( R.id.ll );
getExpandableListView( ).addHeaderView( ll );
我试图摆弄布局中所有组件的 layout_width 或 height,但它不起作用。最让我困惑的是,当它崩溃时,DDMS 上会说NullPointerException
调用 setAdapter 时(当我使用代码中的布局时不会这样,并且它添加了适配器就好了):
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.stephan.test/org.stephan.test.ExpandableListViewTest}: java.lang.NullPointerException
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2833)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.access$2300(ActivityThread.java:136)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.os.Looper.loop(Looper.java:143)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.main(ActivityThread.java:5068)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at java.lang.reflect.Method.invoke(Method.java:521)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at dalvik.system.NativeStart.main(Native Method)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): Caused by: java.lang.NullPointerException
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.widget.ListView.clearRecycledState(ListView.java:506)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.widget.ListView.resetList(ListView.java:492)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.widget.ListView.setAdapter(ListView.java:424)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ExpandableListActivity.setListAdapter(ExpandableListActivity.java:246)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at org.stephan.test.ExpandableListViewTest.onCreate(ExpandableListViewTest.java:80)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1066)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2797)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): ... 11 more
所以,我的问题是:我在这里做错了什么?并且,以防万一,关于如何更好地做到这一点的任何建议?