Android ListView addHeaderView() nullPointerException for predefined Views defined in XML
EDIT:
you simply cannot do
View header = findViewById(R.layout.headerView);
lst.addHeaderView(header);
This will NOT work because the view which is being passed in has to be inflated
. In a nutshell when you do setContentView
at the beginning of your activity the android framework automatically inflates the view and puts it to use. In order to inflate your header view, all you have to do is
View header = (View)getLayoutInflater().inflate(R.layout.headerView,null);
ls.addHeaderView(header);
lastly, add your adapter after you’ve set the header view and run the application. You should see your header view with the content you put into your adapter.
In my case, this works
View header = getLayoutInflater().inflate(R.layout.header, null);
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addHeaderView(header);
listView.addFooterView(footer);
setListAdapter(new ArrayAdapter<String(this,android.R.layout.simple_list_item_single_choice,android.R.id.text1, names));