我正在開發和4.0.3上的Android應用程序,並有幾個使用它們自己的CursorAdapters的ListActivities。如果我只使用一個自定義的CursorAdapter,那麼一切正常,但是當我使用更多的應用程序崩潰時。使用幾個定製的遊標適配器
錯誤堆棧:
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>
ConferenceListCurosrAdapter的第38行是什麼?我只在您發佈的內容中看到32行... – Barak 2012-04-10 14:21:57
這是否僅在活動重新啓動時發生? ESP調試告訴我'test'可能是空的。 – Qberticus 2012-04-10 15:02:16
+1用於ESP – Barak 2012-04-10 15:17:45