NullPointerException停止listview时出错

时间:2013-05-04 15:15:01

标签: java android mysql

您好我正在尝试从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);

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

1 个答案:

答案 0 :(得分:0)

更改

findViewById(R.id.list);

findViewById(android.R.id.list);

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