我希望屏幕具有带标题的列表视图。
在我的XML文件中,我有一个列表视图,在它的正上方有一个图像视图,该图像视图用作标题。
当我运行此程序时,出现错误。我已经看了几十个教程,但看不到我做错了什么。我已经搜索了Stackoverflow,并都尝试了解决方案,但并不高兴。有人可以帮我吗?
注意:我正在使用Actionbarsherlock,因此我的类扩展了SherlockListActivity。我已经尝试使用ListActivity并得到相同的问题。我也尝试过在不实例化图像的情况下运行,以查看listview是否加载自身,但仍然出现相同的错误。
请在下面查看我的XML和代码:
我的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/headerimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/header" >
</ImageView>
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
我的代码:
public class MainActivity extends SherlockListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView header = (ImageView) findViewById(R.id.headerimage);
ListView listView = (ListView) findViewById(R.id.mylist);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}
错误记录
E/AndroidRuntime(10642): FATAL EXCEPTION: main
E/AndroidRuntime(10642): java.lang.RuntimeException: Unable to start activity ComponentInfo{ttj.android.t3w/ttj.android.t3w.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(10642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
E/AndroidRuntime(10642): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
E/AndroidRuntime(10642): at android.app.ActivityThread.access$600(ActivityThread.java:127)
E/AndroidRuntime(10642): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
E/AndroidRuntime(10642): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(10642): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(10642): at android.app.ActivityThread.main(ActivityThread.java:4507)
E/AndroidRuntime(10642): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(10642): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(10642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
E/AndroidRuntime(10642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
E/AndroidRuntime(10642): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(10642): Caused by: java.lang.NullPointerException
E/AndroidRuntime(10642): at ttj.android.t3w.MainActivity.onCreate(MainActivity.java:40)
E/AndroidRuntime(10642): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(10642): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
E/AndroidRuntime(10642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
E/AndroidRuntime(10642): ... 11 more
最佳答案
设置页眉和设置页脚采用夸张的布局。这意味着为标题创建单独的布局并对其进行充气。与设置内容视图类似,但不会自动发生膨胀。一侧不在此处,但显然您需要在设置列表适配器之前设置页眉和页脚。一分钟后,我将尝试发布一些代码。