Reputation: 335
I'm using ListActivity, listview.
listView = getListView();
just working perfectly. I added footer view as
LayoutInflater inflater = getLayoutInflater();
listView.addFooterView( inflater.inflate( R.layout.footer, null ), null, false);
and everything was shiny but ugly, so i wanted to add this footer view (which contains only 1 edittext and only 1 button ) to header of listView as
LayoutInflater inflater = getLayoutInflater();
listView.addHeaderView( inflater.inflate( R.layout.footer, null ), null, false);
and suddenly everything goes wrong, and i get RuntimeException immediately.
Suspended(exception RuntimeException)
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent)
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent)
ActivityThread.access$2200(ActivityThread, Activity$ActiviyRecord, Intent),
so on..
Why is it throws exception ? What is different between addFooterView and addHeaderView, and how can i add Header to ListActivity ?
UPDATE
So as you can read in comments, my logcat still doesn't work, but i just tried next at this moment:
} catch(Exception e){
Writer result = new StringWriter();
PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
String error = result.toString();
}
and afterward i put breakpoint, and i can read error in expressions section. it said :
java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
it was instructive for all of us. After change sort of commands, it works perferctly.
Upvotes: 2
Views: 7037
Reputation: 531
Create layout xml for header and footer
header_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="header"
/>
</RelativeLayout>
footer_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="footer"
/>
</RelativeLayout>
now in activity java file onCreate() method add
listView = (ListView) findViewById(R.id.listView1);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, listView,
false);
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView,
false);
listView.addHeaderView(header, null, false);
listView.addFooterView(footer, null, false);
listView.setAdapter(adapter);
Upvotes: 0
Reputation: 335
so if you want to add header view to your listView, you should do that severely before use setListAdapter() or else it causes an IllegalStateException.
Upvotes: -6
Reputation: 6892
As you log
java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
Listview's method addHeaderView or addFooterView must be called before setAdapter.
Upvotes: 11