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.
NullPointerException at addHeaderView and setAdapter
-
06-10-2022 - |
Question
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
OTHER TIPS
In getView, change
holder.at = (TextView) row.findViewById(R.id.customheaderbutton1);
holder.bt = (TextView) row.findViewById(R.id.customheaderbutton2);
holder.ct = (TextView) row.findViewById(R.id.customheaderbutton3);
to
holder.at = (TextView) row.findViewById(R.id.elementIDtext);
holder.bt = (TextView) row.findViewById(R.id.elementName);
holder.ct = (TextView) row.findViewById(R.id.elementSummary);
Replace your Code With Below:
package com.example.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class List extends Activity
{
private ListView listview;
Veg Veg = new Veg();
ArrayList<Veg> data = new ArrayList<Veg>();
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listview = (ListView)findViewById(R.id.listView);
for(int i=1;i<101;i++)
{
Veg = new Veg();
Veg.setName("Item "+i);
Veg.setID("ID "+i);
Veg.setPrice("Price "+i);
data.add(Veg);
}
View headerView = getLayoutInflater().inflate(
R.layout.custom_list_header, null);
listview.addHeaderView(headerView);
adapter = new CustomAdapter(this,R.layout.custom_list_rows,data);
listview.setAdapter(adapter);
}
class CustomAdapter extends ArrayAdapter<Veg>
{
Context con;
int Res;
ArrayList<Veg> data;
public CustomAdapter(Context context, int resource, ArrayList<Veg> data) {
super(context, resource,data);
// TODO Auto-generated constructor stub
con = context;
Res = 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 = getLayoutInflater();
row = inflater.inflate(Res, parent, false);
holder = new CustomHolder();
holder.at = (TextView) row.findViewById(R.id.elementIDtext);
holder.bt = (TextView) row.findViewById(R.id.elementName);
holder.ct = (TextView) row.findViewById(R.id.elementSummary);
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;
}
class CustomHolder {
TextView at;
TextView bt;
TextView ct;
}
}
}
Also You have to Change your XML Something Like below instead giving Static Width like 11 dp.
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"
android:orientation="horizontal" >
<TextView
android:id="@+id/elementIDtext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/elementName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="elementName"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/elementSummary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="elementValue"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn" />
</LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow