1

我想我已经准备好跳下悬崖了。我一直在尝试制作预加载的 sqlite 数据库以进行批量插入。无论我的数据库文件是什么,都永远不会打开并且应用程序崩溃。最后,我尝试在这里使用最防白痴的方法,但我仍然很短:https ://github.com/jgilfelt/android-sqlite-asset-helper

这是相关的logcat:

09-11 13:41:34.359: I/Database(2913): sqlite returned: error code = 14, msg = cannot open file at source line 25467
09-11 13:41:34.359: E/Database(2913): sqlite3_open_v2("/data/data/com.anthonyce.mcathomie/databases/northwind", &handle, 2, NULL) failed
09-11 13:41:34.369: W/SQLiteAssetHelper(2913): could not open database northwind - unable to open database file
09-11 13:41:34.369: W/SQLiteAssetHelper(2913): copying database from assets...
09-11 13:41:34.369: W/SQLiteAssetHelper(2913): extracting file: 'Northwind-v1.db'...
09-11 13:41:34.779: W/SQLiteAssetHelper(2913): database copy complete
09-11 13:41:34.869: I/SQLiteAssetHelper(2913): successfully opened database northwind
09-11 13:41:34.889: D/AndroidRuntime(2913): Shutting down VM
09-11 13:41:34.889: W/dalvikvm(2913): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-11 13:41:34.910: E/AndroidRuntime(2913): FATAL EXCEPTION: main
09-11 13:41:34.910: E/AndroidRuntime(2913): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anthonyce.mcathomie/com.anthonyce.mcathomie.GameActivity}: java.lang.ClassCastException: android.database.sqlite.SQLiteCursor
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.os.Looper.loop(Looper.java:123)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at java.lang.reflect.Method.invoke(Method.java:507)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at dalvik.system.NativeStart.main(Native Method)
09-11 13:41:34.910: E/AndroidRuntime(2913): Caused by: java.lang.ClassCastException: android.database.sqlite.SQLiteCursor
09-11 13:41:34.910: E/AndroidRuntime(2913):     at com.anthonyce.mcathomie.GameActivity.onCreate(GameActivity.java:25)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-11 13:41:34.910: E/AndroidRuntime(2913):     ... 11 more

编码。我不骗你,我使用的是完全相同的示例文件。我已经被一步一步地复制了样本,但我仍然遇到这些令人沮丧的错误。

public class McatDatabase extends SQLiteAssetHelper{

private static final String DB_NAME = "northwind";
private static final int DB_VERSION = 1;
private static final String[] COLUMNS = {"LastName"};
private static final String TABLE_NAME = "Employees";

public McatDatabase(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

public Cursor getQuestions() {
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(TABLE_NAME, COLUMNS, null, null, null, null, null);
c.moveToFirst();
return c;
}

}

在另一个班级:

public class GameActivity extends Activity {

    private McatDatabase db;
    private Cursor thequestions;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        db = new McatDatabase(this);
        thequestions = db.getQuestions();

        TextView tv = (TextView)findViewById(R.id.TextView1);
        tv.setText((CharSequence) thequestions);
        thequestions.close();
        db.close();
    }

}
4

1 回答 1

0

您正在尝试将光标投射到 CharSequence,而系统不喜欢这样,因此向您抛出 ClassCast 异常。

如果将 Cursor 放入 SimpleCursorAdapter,则可以使用convertToString方法进行转换。否则,您需要自己遍历光标进行转换。

字面上的问题得到了回答,在我看来你正在尝试列出问题,所以我想知道你为什么使用你现在使用的方法而不是使用适配器并创建一个列表(这是 Android非常好的在)。

如果您是该框架的新手并且不了解如何操作,那么这里有很多教程,当您遇到困难时,您可以来这里寻求帮助以克服困难。

于 2012-09-11T14:41:09.537 回答