Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 停止listview时出现NullPointerException错误_Java_Android_Mysql - Fatal编程技术网

Java 停止listview时出现NullPointerException错误

Java 停止listview时出现NullPointerException错误,java,android,mysql,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

您好,我正在尝试从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);
您能提供的任何帮助/见解都将非常有帮助

findViewById(R.id.list);


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

Suspectview
的第22行是什么?第22行是“list.setAdapter(adapter)”;“
list
null
。您是否正在扩展
列表活动
?或者您的xml id列表中是否有一个
ListView
?我扩展到
public class Suspectview的类扩展了ListActivity
,我在.xml活动中也有一个
ListView
项该
ListView
的id是什么?
findViewById(android.R.id.list);