布局为listview标题

Ste*_*han 2 android expandablelistview

我有一个包含两个列表的布局.因为你不能把列表放在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>
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试添加标题时出现问题.如果我从代码创建布局,它可以工作,如下所示:

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 );
Run Code Online (Sandbox Code Playgroud)

如果我使用相同的布局,或者甚至比代码中的布局更简单,应用程序甚至会在屏幕上显示之前崩溃.这是我想要添加为上面列表标题的布局:

<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>
Run Code Online (Sandbox Code Playgroud)

和代码:

LinearLayout ll = (LinearLayout)findViewById( R.id.ll );
getExpandableListView( ).addHeaderView( ll );
Run Code Online (Sandbox Code Playgroud)

我试图摆弄布局中所有组件的layout_width或高度,但它不起作用.令我感到困惑的是,当它崩溃时,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
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是:我在这里做错了什么?而且,如果有任何关于如何更好地做到这一点的建议?

Ste*_*han 5

我找到了解决方案.而不是创建一个新的布局,
LinearLayout ll = (LinearLayout)findViewById( R.id.ll );
你需要膨胀它!以下是诀窍:

LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );  
LinearLayout ll = (LinearLayout)layoutInflater.inflate( R.layout.ll, null, false );
getExpandableListView( ).addHeaderView( ll );
Run Code Online (Sandbox Code Playgroud)

其中ll.xml是您打算作为标题附加到列表的布局.希望这有助于其他人!:)