I have been following this example on creating a multirow list-view on android. However, I get the error
Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
in the line marked below. As far as I see I have followed the example as it is described, but maybe I have missed something obvious.
The relevant pieces of the code are as follows:
Excerpt of the code to show the view (when the user presses a button):
public void stat(View view) {
// First, sort the pile of entries
Map<Integer, Integer> testMap = new HashMap<Integer, Integer>();
for (int i = 0; i < quizIndex.size(); i++) {
testMap.put(i, quizIndex.get(i).getPriority());
}
MyComparator comparator = new MyComparator(testMap);
Map<Integer, Integer> sortedMap = new TreeMap<Integer, Integer>(comparator);
sortedMap.putAll(testMap);
// Then, fill out the lists of lists
ArrayList<HashMap<String,String>> adapterList = new ArrayList<HashMap<String,String>>();
int index;
for (Map.Entry<Integer, Integer> mapentry : sortedMap.entrySet())
{
System.out.println(mapentry.getKey() + "/" + mapentry.getValue());
index = mapentry.getKey();
Entry entry = quizIndex.get(index);
System.out.println(String.format("%30s %d %d %s %d %d %d",
entry.name(), entry.number_ok, entry.number_nok, entry.history, entry.randomIndex, entry.histIndex, entry.getPriority()));
HashMap<String,String> temp=new HashMap<String, String>();
temp.put(COLUMN_NAME, entry.name());
temp.put(COLUMN_OK, Integer.toString(entry.number_ok));
temp.put(COLUMN_NOK, Integer.toString(entry.number_nok));
temp.put(COLUMN_HIST, entry.history);
temp.put(COLUMN_PRANDOM, Integer.toString(entry.randomIndex));
temp.put(COLUMN_PHIST, Integer.toString(entry.histIndex));
temp.put(COLUMN_PTOT, Integer.toString(entry.getPriority()));
adapterList.add(temp);
}
// Finally, create the view
ListView listView=(ListView)findViewById(R.id.listview1);
StatViewAdapter adapter=new StatViewAdapter((Activity)this, adapterList);
listView.setAdapter(adapter); // ERROR HERE
}
And here is the complete StatViewAdapter
:
public class StatViewAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtName;
TextView txtOK;
TextView txtNOK;
TextView txtHist;
TextView txtPrandom;
TextView txtPhist;
TextView txtPtotal;
public StatViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.list_view, null);
txtName=(TextView) convertView.findViewById(R.id.listname);
txtOK=(TextView) convertView.findViewById(R.id.listok);
txtNOK=(TextView) convertView.findViewById(R.id.listnok);
txtHist=(TextView) convertView.findViewById(R.id.listhist);
txtPrandom =(TextView) convertView.findViewById(R.id.listprandom);
txtPhist=(TextView) convertView.findViewById(R.id.listphist);
txtPtotal=(TextView) convertView.findViewById(R.id.listptot);
}
HashMap<String, String> map=list.get(position);
txtName.setText(map.get(COLUMN_NAME));
txtOK.setText(map.get(COLUMN_OK));
txtNOK.setText(map.get(COLUMN_NOK));
txtHist.setText(map.get(COLUMN_HIST));
return convertView;
}
}
How can it be a null-reference error? I define a valid object, as far as I see, which is not-null...?
Addendum: The 'listview1`:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>