I’m developing and Android App on 4.0.3 and have several ListActivities using their own CursorAdapters. If I’m using only one customized CursorAdapter everything works fine but when I’m using more my application crashes.
Error stack:
04-10 15:41:08.897: W/dalvikvm(2239): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-10 15:41:08.956: E/AndroidRuntime(2239): FATAL EXCEPTION: main
04-10 15:41:08.956: E/AndroidRuntime(2239): java.lang.NullPointerException
04-10 15:41:08.956: E/AndroidRuntime(2239): at com.tsystems.openconf.database.adapter.ConferenceListCursorAdapter.bindView(ConferenceListCursorAdapter.java:38)
04-10 15:41:08.956: E/AndroidRuntime(2239): at android.widget.CursorAdapter.getView(CursorAdapter.java:250)
...
It seems that Android can’t handle several ListActivities.
All of my inherited ListActivity classes include the following code, except of the CursorAdapter class:
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate called");
super.onCreate(savedInstanceState);
Log.d(TAG, "create DatabaseOpenHelper");
DatabaseOpenHandler helper = new DatabaseOpenHandler(this);
Log.d(TAG, "get writeable database access");
database = helper.getWritableDatabase();
Log.d(TAG, "create Cursor for database access");
data = database.query(DatabaseConstants.TABLE_CONFERENCES, fields,
null, null, null, null, null);
Log.d(TAG, "set ConferenceListCursorAdapter");
setListAdapter(new ConferenceListCursorAdapter(this, data));
}
My customized inherited CursorAdapter looks like this and overwrites the bindView and newView method:
public class ConferenceListCursorAdapter extends CursorAdapter {
// logging identifier
private static final String TAG = ConferenceListCursorAdapter.class.getSimpleName();
private Cursor cursor;
private Context context;
private final LayoutInflater inflater;
public ConferenceListCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
this.inflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(TAG, "bindView of ConferenceListAdapter called");
TextView test = (TextView) view.findViewById(R.id.conference_list_id);
test.setText(Long.toString(cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCES_STARTTIMEINMILLIS))));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.conference_list_item, parent, false);
}
}
EDIT
In line 38 is written:
test.setText(Long.toString(cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCES_STARTTIMEINMILLIS))));
It looks like Android can’t find the TextView and therefore throws the NPE when setting the text. The id of the test TextView is written in the R class and should be found by Android. So why does it throws the NPE?
code snippet of my layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/conference_list_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
I fixed the problem by using ArrayAdapter instead of CursorAdapter. I had to change a lot but now everything works fine.