I am trying to create a listview with headers based on a number of predefined categories. I am using Jess Sharkey's separating lists with headers as a basis. The classes and xml are identical except for SimpleAdapter. The difference is my data is coming from a local sqlite database. The code crashes when I switch out of the view. Any ideas why this could be? Error and code below.
07-15 14:58:44.950: ERROR/AndroidRuntime(582): Uncaught handler: thread main exiting due to uncaught exception
07-15 14:58:44.970: ERROR/AndroidRuntime(582): java.lang.NullPointerException
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:149)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at com.XYZ.XYZ.SeparatedListAdapter.getView(SeparatedListAdapter.java:94)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.AbsListView.obtainView(AbsListView.java:1256)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.ListView.makeAndAddView(ListView.java:1668)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.ListView.fillDown(ListView.java:637)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.ListView.fillSpecific(ListView.java:1224)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.ListView.layoutChildren(ListView.java:1499)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.View.layout(View.java:6830)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.View.layout(View.java:6830)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.View.layout(View.java:6830)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.View.layout(View.java:6830)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.View.layout(View.java:6830)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.View.layout(View.java:6830)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.View.layout(View.java:6830)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.View.layout(View.java:6830)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.os.Looper.loop(Looper.java:123)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at java.lang.reflect.Method.invoke(Method.java:521)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-15 14:58:44.970: ERROR/AndroidRuntime(582): at dalvik.system.NativeStart.main(Native Method)
Category.java (My equivalent to SimpleAdapter from Starkeys tutorial.)
package com.XYZ.ipantrylite;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Category extends Activity {
public final static String ITEM_TITLE = "title";
public final static String ITEM_CAPTION = "caption";
public SeparatedListAdapter adapter;
private static final String TAG = "Category";
public Map<String,?> createItem(String title, String caption) {
Map<String,String> item = new HashMap<String,String>();
item.put(ITEM_TITLE, title);
item.put(ITEM_CAPTION, caption);
return item;
}
@Override
// protected void onCreate(Bundle savedInstanceState) {
// // TODO Auto-generated method stub
// super.onCreate(savedInstanceState);
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// create our list and custom adapter
adapter = new SeparatedListAdapter(this);
fillData();
ListView list = new ListView(this);
list.setTextFilterEnabled(false);
list.setAdapter(adapter);
this.setContentView(list);
}
private void fillData()
{
String[] types = new String[] {
"Baby Food",
"Baking Supplies",
"Beverages",
"Dairy Products & Eggs",
"Fresh Produce, Fruits & Vegetables",
"Canned, Dried, Frozen Goods",
"Grains, Cereals, Breads, Pasta",
"Herbs & Spices",
"Misc Canned Goods",
"Oils & Fats",
"Prepackaged Foods",
"Protein, Meat, Fish",
"Snacks & Sweets",
"Soups, Sauces, & Gravies",
"Toiletries"};
for(int i = 0; i < types.length - 1; i++)
{
Cursor c = managedQuery(PantryItemsProviderLite.CONTENT_URI, null, "TYPE = '"+types[i]+"'", null, "name desc");
startManagingCursor(c);
if (c.moveToFirst()) {
c.setNotificationUri(getContentResolver(), PantryItemsProviderLite.CONTENT_URI);
String[] from = new String[] { PantryItemsProviderLite.NAME};
int[] to = new int[] { R.id.textName};
SimpleCursorAdapter items = new SimpleCursorAdapter(this, R.layout.items_row_cat, c, from, to);
adapter.addSection(types[i], items);
}
adapter.notifyDataSetChanged();
}
}
@Override
protected void onPause(){
super.onPause();
clearData();
Log.d("","on Pause called");
}
@Override
protected void onResume() {
super.onResume();
fillData();
Log.d("","on Resume called");
}
public void clearData(){
adapter.headers.clear();
adapter.sections.clear();
}
}
Line of code referred to in debugger at crash.
Line 94
if(position < size) return adapter.getView(position - 1, convertView, parent);