Android listview with header

dmiles

New Member
I would like my screen to have a listview with a header.In my XML file, I have a listview and just above it there is an imageview, which is being used as the header.When I run this I get an error. I have gone through dozens of tutorials and I can't see what I am doing wrong. I have searched Stackoverflow and all tried the solutions but had no joy. Can anybody help me please?Note: I am using Actionbarsherlock, so my class extends SherlockListActivity. I have tried this with ListActivity and get the same problem. I have also tried running without instantiating the image to see if the listview loads itself, and still I get the same error.Please see my XML and code below:My XML\[code\]<?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="http://stackoverflow.com/questions/10989224/@drawable/header" ></ImageView><ListView android:id="@+id/mylist" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView>\[/code\]My code:\[code\]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);}\[/code\]Error log\[code\]E/AndroidRuntime(10642): FATAL EXCEPTION: mainE/AndroidRuntime(10642): java.lang.RuntimeException: Unable to start activity ComponentInfo{ttj.android.t3w/ttj.android.t3w.MainActivity}: java.lang.NullPointerExceptionE/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.NullPointerExceptionE/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\[/code\]
 
Back
Top