【问题标题】:Android ArrayAdapter crashing when inserting插入时Android ArrayAdapter崩溃
【发布时间】:2020-01-09 22:40:32
【问题描述】:

autoCompleteTextView 中插入数据库中的数据时应用程序崩溃。

我有两节课

首先,测试类

public class Test extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    final DataBaseHelper myDb = new DataBaseHelper(this);
    final String [] myData = myDb.SelectAllData();
    final AutoCompleteTextView autoCom = findViewById(R.id.stations);
    ArrayAdapter arrayAdapter=new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, myData);
    autoCom.setAdapter(arrayAdapter);
}}

二、DataBaseHelper类

public class DataBaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "stations";
private static final String TABLE_STATIONS = "stations";

public DataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
}

public String[] SelectAllData() {
    try {
        String arrData[] = null;
        SQLiteDatabase db;
        db = this.getReadableDatabase();
        String strSQL = "SELECT name FROM " + TABLE_STATIONS;
        Cursor cursor = db.rawQuery(strSQL, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                arrData = new String[cursor.getCount()];
                int i = 0;
                do {
                    arrData[i] = cursor.getString(0);
                    i++;
                } while (cursor.moveToNext());
            }
        }
        cursor.close();
        return arrData;
    } catch (Exception e) {
        return null;
    }
}


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

}}

【问题讨论】:

  • 如果可以的话,请发布堆栈跟踪,它有很大帮助。

标签: java android sqlite android-studio android-arrayadapter


【解决方案1】:

问题

我相信您的问题是您将遇到 table not found 错误,因为数据库不包含任何表。但是,您正在捕获异常并且不采取任何措施,因此应用程序似乎无法正常工作。

如果你要添加

e.printStackTrace();

return null 之前,日志会包含类似的内容:-

2020-01-10 11:32:17.856 E/SQLiteLog: (1) no such table: stations
2020-01-10 11:32:17.856 W/System.err: android.database.sqlite.SQLiteException: no such table: stations (code 1 SQLITE_ERROR): , while compiling: SELECT name FROM stations
2020-01-10 11:32:17.859 W/System.err:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
2020-01-10 11:32:17.859 W/System.err:     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
2020-01-10 11:32:17.859 W/System.err:     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
2020-01-10 11:32:17.859 W/System.err:     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
2020-01-10 11:32:17.860 W/System.err:     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
2020-01-10 11:32:17.860 W/System.err:     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
2020-01-10 11:32:17.860 W/System.err:     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
2020-01-10 11:32:17.860 W/System.err:     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1443)
2020-01-10 11:32:17.860 W/System.err:     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1382)
2020-01-10 11:32:17.860 W/System.err:     at a.so59673151notable.DataBaseHelper.SelectAllData(DataBaseHelper.java:27)
2020-01-10 11:32:17.860 W/System.err:     at a.so59673151notable.MainActivity.onCreate(MainActivity.java:14)
  • 捕获 SQLite 异常并不是一个好主意,因为它们通常会是严重的错误,并且从您自己的经验中可以看出,当您认为问题在于未构建数组时会感到困惑。李>

修复

第 1 部分

您应该使用 DataBaseHelper 类的 onCreate 方法来创建 stations 表。

例如

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_STATIONS + "(name TEXT);");
}
  • 当然,这可能不是您需要的架构,它只是一个示例。您不妨参考CREATE TABLE

第 2 部分

由于数据库已创建,DataBaseHelper 的 onCreate 方法将不会运行,因为它仅在创建数据库时运行一个。因此,您需要删除数据库。在开发最简单的方法时,可以:-

  1. 删除/清除应用数据
  2. 卸载应用程序

之后您可以重新运行该应用程序。

更多

要从数据库中获取数据,您需要将数据添加到数据库中,因此您需要按照以下方式向 DataBaseHelper 类添加一个方法(再次假设是上面创建的基本表): -

public long insertStation(String name) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("name",name);
    return db.insert(TABLE_STATIONS,null,cv);
}

您可能希望阅读insert

然后您可以在 Test 类中包含以下内容:-

final DataBaseHelper myDb = new DataBaseHelper(this); //<<<<< EXISTING LINE
myDb.insertStation("MyFirstStation");
myDb.insertStation("MySecondStation");
final String [] myData = myDb.SelectAllData(); //<<<<< EXISTING LINE
  • 请注意,这只是为了测试,它会在每次运行应用程序时添加相同的数据,因此每次运行都会多出 2 行。

测试

以上更改已通过使用 :-

进行了测试
    final DataBaseHelper myDb = new DataBaseHelper(this);
    myDb.insertStation("MyFirstStation");
    myDb.insertStation("MySecondStation");
    final String [] myData = myDb.SelectAllData();
    for (String s: myData) {
        Log.d("STATIONINFO","Station Name is " + s);
    }

这会导致(安装应用程序后的第一次运行):-

2020-01-10 11:49:42.321 D/STATIONINFO: Station Name is MyFirstStation
2020-01-10 11:49:42.321 D/STATIONINFO: Station Name is MySecondStation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多