私は4.0.3でAndroidアプリを開発しており、独自のCursorAdaptersを使用していくつかのListActivitiesを持っています。カスタマイズされた CursorAdapter を 1 つだけ使用している場合はすべて正常に動作しますが、それ以上使用するとアプリケーションがクラッシュします。
エラー スタック:
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)
...
Android は複数の ListActivities を処理できないようです。
継承したすべての ListActivity クラスには、CursorAdapter クラスを除き、次のコードが含まれています。
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));
}
カスタマイズされた継承された CursorAdapter は次のようになり、bindView メソッドと newView メソッドを上書きします。
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);
}
}
編集
38行目に次のように書かれています。
test.setText(Long.toString(cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCES_STARTTIMEINMILLIS))));
Android は TextView を見つけることができないため、テキストを設定するときに NPE をスローするようです。テスト TextView の ID は R クラスに記述されており、Android によって検出される必要があります。では、なぜ NPE がスローされるのでしょうか。
私のレイアウトxmlファイルのコードスニペット:
<?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>