1

私は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>
4

2 に答える 2

0

CursorAdapter の代わりに ArrayAdapter を使用して問題を修正しました。多くの変更が必要でしたが、今ではすべて正常に機能しています。

于 2012-04-12T11:11:23.857 に答える
0

投稿したコードからいくつかのコードを除外したと推測すると、その 38 行目は実際には

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

cursor.moveToFirst();カーソルで何かをしようとする前に使用する必要があると思います。カーソルが作成されると、最初のレコードの前に配置されます。その状態で使用しようとすると、NPE が発生します。

于 2012-04-10T14:18:40.453 に答える