Java 无法将SqlLiteDatabase中的数据绑定到simplecursoradaptor

Java 无法将SqlLiteDatabase中的数据绑定到simplecursoradaptor,java,android,Java,Android,我不知道如何将数据连接到我的数组字符串[]station={“København”、“Grenaa”、“hanssholm”};在我对SimpleCrsorAdaptor的印象中 我制作了一个SQLitedatabase、一个Helperclass和一个ActivityClass,但是我得到了错误“java.lang.IllegalArgumentException:列'København'不存在”。我有额外的代码-但我认为这个代码应该足够了。 任何帮助都将不胜感激 public class M

我不知道如何将数据连接到我的数组字符串[]station={“København”、“Grenaa”、“hanssholm”};在我对SimpleCrsorAdaptor的印象中 我制作了一个SQLitedatabase、一个Helperclass和一个ActivityClass,但是我得到了错误“java.lang.IllegalArgumentException:列'København'不存在”。我有额外的代码-但我认为这个代码应该足够了。 任何帮助都将不胜感激

public class MyListActivity extends ListActivity {
String [] station = {"København", "Grenaa", "Hanstholm"};
Cursor stations;
SQLiteDatabase db;
SimpleCursorAdapter cursorAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DBAdapter  dbaAdapter = new DBAdapter(this);
    dbaAdapter.open();
    Cursor stations = dbaAdapter.getStations();

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,stations,station,new int [] {
            android.R.id.text1
    });
    setListAdapter(cursorAdapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Cursor cursor = (Cursor) l.getItemAtPosition(position);
    String value = station[(int)id];
    Intent intent = new Intent();
    intent.putExtra(TravelActivity.SELECTED_STATION_NAME, cursor.getString(cursor.getColumnIndexOrThrow("station")));
    this.setResult(RESULT_OK,intent);

    cursor.close();
    finish();


}

@Override
protected void onDestroy() {
    db.close();

}
}


该错误由SimpleCursorAdapter构造函数生成:

SimpleCursorAdapter cursorAdapter=新的SimpleCursorAdapter(此, android.R.layout.simple_list_item_1,stations,station,new int[]{ android.R.id.text1 });


4td参数是列名,因此在您的示例中是一个带有START和/或SLUT值的字符串数组

你到底是从哪里得到错误的?在使用游标值之前,您是否正在执行cursor.moveToNext/MoveToFirst?您的数据库帮助程序类不完整,请通过google了解它并了解有关sqlite的更多信息。我在编译程序时遇到错误。您能否发布新的构造函数行代码和完整的logcat错误?请@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);DBAdapter DBAdapter=newdbadapter(this);DBAdapter.open();Cursor stations=DBAdapter.getStations();SimpleCursorAdapter cursorAdapter=newsimplecursoradapter(this,android.R.layout.simple_list_项_1,stations,START,new int[]{android.R.id.text1});setListAdapter(cursorAdapter);}我不能用这个新构造函数编译,因为MyListActivity类中没有起始引用,所以我可以显示原始错误以及您的建议(据我所知)。我还必须解释,事实上我能够用我的原始代码进行编译-当我在MyLisActivity中单击带有lstener的按钮时,它会崩溃SimpleCrsOrAdapter cursorAdapter=new SimpleCrsOrAdapter(这个,android.R.layout.simple_list_item_1,stations,START,new int[]{android.id.text1(我在上一条评论中超过了字符数-因此我不得不将其拆分为)logCat(在原始代码中)java.lang.RuntimeException:无法实例化活动组件信息{com.example.utzon.myapplicationlessonfive/com.example.utzon.myapplicationlessonfive.MyListActivity}:java.lang.NullPointerException:尝试在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)上的空对象引用上读取字段“java.lang.String com.example.utzon.myapplicationlessonfive.DBAdapter.START”使开始公共、静态和最终:公共静态最终字符串START=“START”;并使用此构造函数行:SimpleCursorAdapter cursorAdapter=new SimpleCursorAdapter(这是android.R.layout.simple_list_item_1,stations,新字符串[]{DBAdapter.START},新int[]{android.R.id.text1});
public class MyHelper extends SQLiteOpenHelper {

  public static final String DB_NAME = "database";
    String DESTINATION = "DESTINATION";
    int version = 1;



    public MyHelper(Context context) {

        super(context, "travel.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String TRAVELS = ( "create table travels (_id integer primary key autoincrement, start text, slut text)");
        String STATIONS = ( "create table stations (_id integer primary key autoincrement, start text)" );
        db.execSQL(TRAVELS);
        db.execSQL(STATIONS);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS ");
        onCreate(db);

    }
}



public class DBAdapter {
    MyHelper myHelper;
    SQLiteDatabase db;

    String TABLE_STATIONS = "stations";
    String TABLE_TRAVELS = "travels";
    String START = "start";
    String SLUT = "slut";
    String ID_COL = "_id";
    Context context;

    public static final int NUMBER_TRAVELS = 1;


    public DBAdapter(Context context) {
       this.context = context;
        myHelper = new MyHelper(context);

    }

    public void open() {
        db = myHelper.getWritableDatabase();

    }

    public void close() {
        myHelper.close();
    }

    public Cursor getTravels() {
        Cursor cursor = db.query(TABLE_TRAVELS,new String[]{ID_COL,START,SLUT},null,null,null,null,START);
      return cursor;
    }


    public void saveTravels(String start, String slut) {
        ContentValues values = new ContentValues();
        values.put(START,start);
        values.put(SLUT,slut);
        db.insert(TABLE_TRAVELS,null,values);



    }

    public Cursor getStations() {
        Cursor cursor = db.query(TABLE_STATIONS,new String[]{ID_COL,START},null,null,null,null,START);
        return cursor;

    }

    public void saveStations(String start) {
        ContentValues values = new ContentValues();
        values.put(START,start);
        db.insert(TABLE_TRAVELS,null,values);
    }

}