我想在我的列表视图中添加一个标题,但它正在强制关闭。我正在尝试将标题放在列表视图的顶部。因此,当我创建此列表视图时,我将操作这些代码:
lv = getListView();
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
这是我的 header.xml :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#DCDCDC"
android:orientation="vertical" >
<requestFocus />
<ImageView
android:id="@+id/imageButton2"
android:layout_width="fill_parent"
android:layout_height="52dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/header"
android:src="@drawable/list_header" />
<ImageButton
android:id="@+id/imageButtonSearch"
android:layout_width="60dip"
android:layout_height="45dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/header"
android:src="@drawable/search"
android:background="@android:color/transparent"
/>
<ImageView
android:id="@+id/imageButton3"
android:layout_width="150dip"
android:layout_height="50dip"
android:layout_alignBottom="@+id/imageButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/header"
android:src="@drawable/titel" />
</RelativeLayout>
这是我的日志:
12-04 19:31:30.478: E/AndroidRuntime(372): FATAL EXCEPTION: main
12-04 19:31:30.478: E/AndroidRuntime(372): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.laurenswuyts.find.it/com.laurenswuyts.find.it.AndroidListViewActivity}: java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
12-04 19:31:30.478: E/AndroidRuntime(372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-04 19:31:30.478: E/AndroidRuntime(372): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-04 19:31:30.478: E/AndroidRuntime(372): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 19:31:30.478: E/AndroidRuntime(372): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-04 19:31:30.478: E/AndroidRuntime(372): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 19:31:30.478: E/AndroidRuntime(372): at android.os.Looper.loop(Looper.java:130)
12-04 19:31:30.478: E/AndroidRuntime(372): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-04 19:31:30.478: E/AndroidRuntime(372): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 19:31:30.478: E/AndroidRuntime(372): at java.lang.reflect.Method.invoke(Method.java:507)
12-04 19:31:30.478: E/AndroidRuntime(372): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-04 19:31:30.478: E/AndroidRuntime(372): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-04 19:31:30.478: E/AndroidRuntime(372): at dalvik.system.NativeStart.main(Native Method)
12-04 19:31:30.478: E/AndroidRuntime(372): Caused by: java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
12-04 19:31:30.478: E/AndroidRuntime(372): at android.widget.ListView.addHeaderView(ListView.java:279)
12-04 19:31:30.478: E/AndroidRuntime(372): at com.laurenswuyts.find.it.AndroidListViewActivity.onCreate(AndroidListViewActivity.java:80)
12-04 19:31:30.478: E/AndroidRuntime(372): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-04 19:31:30.478: E/AndroidRuntime(372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-04 19:31:30.478: E/AndroidRuntime(372): ... 11 more