我想我准备跳下悬崖了。我一直在尝试将预加载的sqlite数据库用于批量插入目的。无论我的db文件从未打开过,应用程序崩溃了。最后,我试图在这里使用最顽固的方法,我仍然做得很简短: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();
}
}
答案 0 :(得分:0)
您正在尝试将光标转换为CharSequence,并且系统不喜欢这样,因此会向您抛出ClassCast异常。
如果将Cursor放入SimpleCursorAdapter,则可以使用convertToString方法进行转换。否则,您需要自己迭代光标进行转换。
字面上的问题回答了,在我看来你正在尝试列出问题列表,所以我想知道你为什么使用你的方法而不是使用适配器并创建一个列表(这是Android的东西) 非常擅长)。
如果您是框架的新手并且不了解如何操作,那么有许多教程,当您遇到困难时,您可以来到这里并向表单寻求帮助来克服困难点。