1

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

<?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>

My 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);




}

Error log

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
tiptopjat
  • 499
  • 3
  • 13
  • 24
  • rather than using `findViewById` to get find the listview from the file, use `getListView()` in a class extending ListActivity. – ataulm Dec 25 '12 at 23:28

4 Answers4

3

Set header and set footer take an inflated layout. That means creating a seperate layout for the header and inflating it. Similar to set content view but the inflation does not happen automatically. One side not here but aparently you need to set the header and footer before setting the list adapter. When I get a minute I will try to post some code.

cstrutton
  • 5,667
  • 3
  • 25
  • 32
  • super.onCreate(savedInstanceState); and setContentView(R.layout.main); are the first two line of the oncreate method. I removed them because all the tutorials I have read do not have it. It's my understanding that listview doesn't need setContentView. Either way, I've tried running the code with it and the error still persists – tiptopjat Jun 12 '12 at 01:01
  • When I use setcontentview I get the following error: E/AndroidRuntime(17767): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' – tiptopjat Jun 12 '12 at 01:35
2

cstrutton is right. You're not calling setConentView You should call setContentView(R.layout.xmlfilename) right after the call to super.onCreate(savedInstanceState);

Where xmlfilename is the name of your layout file

Inn0vative1
  • 2,014
  • 3
  • 27
  • 43
0

setContentView(id) should be used with list activities, and an activity incorporating a ListView ought to extend ListActivity or ListFragment (or an appropriate subclass).

In your comment to cstrutton, you state:

When I use setcontentview I get the following error: E/AndroidRuntime(17767): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

This is because your ListView has an incorrect id - in a ListActivity, there should be a maximum of one ListView represented in the associated layout file, and its id should be @android:id/list (presumably so that Android knows where the ListView is, though I'm unsure why it can't detect it from the element tag, perhaps for performance reasons it's just simpler to require a specific ID).

Change @+id/list to @android:id/list. (see this question for why it's @android:id rather than @+id)

I suppose there are other reasons to use a class extending ListActivity and a lot of them will be for convenience methods, but I suspect many are for optimisations.

Onwards and upwards!

Community
  • 1
  • 1
ataulm
  • 15,195
  • 7
  • 50
  • 92
-1

FIXED.

The activity should NOT extend ListActivity. I changed it to Activity and the problem has gone.

tiptopjat
  • 499
  • 3
  • 13
  • 24
  • -1 using a ListView without extending ListActivity. Change it back to ListActivity, and change the id of the ListView in your layout file from `mylist` to `list` as the error clearly states. – ataulm Dec 25 '12 at 23:32