Reputation: 499
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
Upvotes: 1
Views: 3468
Reputation: 15334
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!
Upvotes: 0
Reputation: 499
FIXED.
The activity should NOT extend ListActivity. I changed it to Activity and the problem has gone.
Upvotes: -1
Reputation: 6187
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.
Upvotes: 3
Reputation: 2145
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
Upvotes: 2