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.
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 .
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);
因此,如果要将标题视图添加到listView,则应在使用setListAdapter()之前严格执行此操作,否则会导致IllegalStateException。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.