使用几个自定义CursorAdapter

时间:2012-04-10 13:51:48

标签: android android-layout

我正在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。

除了CursorAdapter类之外,我所有继承的ListActivity类都包含以下代码:

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>

2 个答案:

答案 0 :(得分:0)

猜测你从你发布的内容中留下了一些代码,而第38行可能实际上是

test.setText(Long.toString(cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCES_STARTTIMEINMILLIS))));

在您尝试对光标执行任何操作之前,我认为您需要使用cursor.moveToFirst();。创建光标时,它位于之前第一条记录。如果您尝试在该状态下使用它,它将为您提供NPE。

答案 1 :(得分:0)

我通过使用ArrayAdapter而不是CursorAdapter来解决问题。我不得不改变很多,但现在一切正常。