Search code examples
androidnullpointerexceptioncustom-lists

NullPointerException at addHeaderView and setAdapter


Note : i have refered similar questions and still cannot find any mistakes


NullPointerException at

listView.addHeaderView(header);

AND

listView.setAdapter(adapter);

detailed code :

Veg v = new Veg();
v.setId(1);
v.setName("some");
v.setPrice(15.0);

List<Veg> vegData = new ArrayList<Veg>();

// dummy add for testing 
vegData.add(v);
vegData.add(v);
vegData.add(v);

CustomAdapter adapter = new CustomAdapter(this,
        R.layout.custom_list_rows, vegData);
listView = (ListView) findViewById(R.id.listView1);


View header = getLayoutInflater().inflate(
        R.layout.custom_list_header, null);

listView.addHeaderView(header);

listView.setAdapter(adapter);

getView() part

public class CustomAdapter extends ArrayAdapter<Veg> {

    Context context;
    int layoutResourceId;
    List<Veg> data;

    public CustomAdapter(Context context, int resource, List<Veg> data) {
        super(context, resource);
        this.context = context;
        this.layoutResourceId = resource;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        CustomHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new CustomHolder();
            holder.at = (TextView) row.findViewById(R.id.customheaderbutton1);
            holder.bt = (TextView) row.findViewById(R.id.customheaderbutton2);
            holder.ct = (TextView) row.findViewById(R.id.customheaderbutton3);
            row.setTag(holder);
        } else {
            holder = (CustomHolder) row.getTag();
        }

        holder.at.setText(String.valueOf(data.get(position).getId()));
        holder.bt.setText(data.get(position).getName());
        holder.ct.setText(String.valueOf(data.get(position).getPrice()));

        return row;
    }

    static class CustomHolder {
        TextView at;
        TextView bt;
        TextView ct;
    }

}

main activity xml file

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"> 

     <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

custom_list_header.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="horizontal" >

    <Button
        android:id="@+id/customheaderbutton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:text="marketname" />

    <Button
        android:id="@+id/customheaderbutton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:text="edit" />

    <Button
        android:id="@+id/customheaderbutton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:text="+" />

</LinearLayout>

custom_list_rows.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" >

    <TextView
        android:id="@+id/elementIDtext"
        android:layout_width="11dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/elementName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.30"
        android:text="@string/elementName"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/elementSummary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/elementValue"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="62dp"
        android:layout_height="wrap_content"
        android:text="@string/_" />

</LinearLayout>

Solution

  • Sounds like listView is null, which means findViewById() returned null: it couldn't find the control you're looking for. Without seeing your layout there's no way to debug further, but look to be sure R.id.listView1 refers to a valid element.