我希望我的屏幕有一个带有标题的列表视图。
在我的 XML 文件中,我有一个列表视图,在它上面有一个图像视图,它被用作标题。
当我运行它时,我得到一个错误。我已经阅读了几十个教程,但我看不出我做错了什么。我搜索了 Stackoverflow 并尝试了所有解决方案,但没有任何乐趣。有人可以帮我吗?
注意:我使用的是 Actionbarsherlock,所以我的类扩展了 SherlockListActivity。我已经用 ListActivity 尝试过这个并遇到同样的问题。我也尝试在不实例化图像的情况下运行以查看列表视图是否自行加载,但仍然出现相同的错误。
请在下面查看我的 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