0

您好,我正在尝试从 SQLITE 数据库填充列表视图。我使用自定义光标类来调整数据,然后扩充我的布局

    public CustomCursorAdapter(Context context, Cursor c) {
    super(context, c);


}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // when the view will be created for first time,
    // we need to tell the adapters, how each item will look
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.suspect_list, parent, false);

    return retView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // here we are setting our data
    // that means, take the data from the cursor and put it in views

    TextView textViewId = (TextView) view.findViewById(R.id.suspect_id);
    textViewId.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(0))));

    TextView textViewPersonName = (TextView) view.findViewById(R.id.suspect_name);
    textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

    TextView textViewPersonSex = (TextView) view.findViewById(R.id.suspect_sex);
    textViewPersonSex.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));

}

运行代码时,通过 logcat 显示错误

    05-04 15:10:05.371: D/AndroidRuntime(444): Shutting down VM
05-04 15:10:05.371: W/dalvikvm(444): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-04 15:10:05.381: E/AndroidRuntime(444): FATAL EXCEPTION: main
05-04 15:10:05.381: E/AndroidRuntime(444): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sherlock/com.example.sherlock.Suspectview}: java.lang.NullPointerException
05-04 15:10:05.381: E/AndroidRuntime(444):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-04 15:10:05.381: E/AndroidRuntime(444):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-04 15:10:05.381: E/AndroidRuntime(444):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-04 15:10:05.381: E/AndroidRuntime(444):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-04 15:10:05.381: E/AndroidRuntime(444):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 15:10:05.381: E/AndroidRuntime(444):  at android.os.Looper.loop(Looper.java:123)
05-04 15:10:05.381: E/AndroidRuntime(444):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-04 15:10:05.381: E/AndroidRuntime(444):  at java.lang.reflect.Method.invokeNative(Native Method)
05-04 15:10:05.381: E/AndroidRuntime(444):  at java.lang.reflect.Method.invoke(Method.java:507)
05-04 15:10:05.381: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-04 15:10:05.381: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-04 15:10:05.381: E/AndroidRuntime(444):  at dalvik.system.NativeStart.main(Native Method)
05-04 15:10:05.381: E/AndroidRuntime(444): Caused by: java.lang.NullPointerException
05-04 15:10:05.381: E/AndroidRuntime(444):  at com.example.sherlock.Suspectview.onCreate(Suspectview.java:22)
05-04 15:10:05.381: E/AndroidRuntime(444):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-04 15:10:05.381: E/AndroidRuntime(444):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-04 15:10:05.381: E/AndroidRuntime(444):  ... 11 more

我知道这个错误是通过对变量的误解而收缩的,我在我的代码中看不到哪里出错了......

数据库游标

public Cursor getAll() {
    String selectQuery = "SELECT  * FROM " + TABLE_SUSPECTS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    return cursor;
}

正在使用适配器的活动

 DatabaseHandler db = new DatabaseHandler(this);
      list = (ListView) findViewById(R.id.list);
      Cursor c = db.getAll();
      CustomCursorAdapter adapter = new CustomCursorAdapter(this, c);
      list.setAdapter(adapter);

您可以提供的任何帮助/见解都会非常有帮助

4

1 回答 1

0

改变

findViewById(R.id.list);

findViewById(android.R.id.list);

您需要添加android命名空间,因为它不是id您创建的

于 2013-05-04T15:36:12.557 回答