我想使用我已经使用SQLite数据库浏览器创建的预加载数据库来填充listview。但是,我不断收紧。对于DatabaseHelper类,我精确地在http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/上跟随了教程
这里是我的代码显示listView:
public class POITab extends ListActivity {
private SQLiteDatabase newDb;
protected Cursor cursor;
protected ListAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openDatabase();
}
private void openDatabase() {
POIHelper dbHelper = new POIHelper(this.getApplicationContext());
newDb = dbHelper.getWritableDatabase();
cursor = newDb.rawQuery("SELECT _id, Name FROM POI", null);
adapter = new SimpleCursorAdapter(this, R.layout.listitem, cursor, new String[] {"Name"}, new int[] {R.id.name});
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}更新:我不知道,但我想问题在于线
cursor = newDb.rawQuery("SELECT _id, Name FROM POI", null);这是我的databaseHelper类的代码:
public class POIHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.test.mysgapp/databases/";
private static String DB_NAME = "MySgAppdb";
private SQLiteDatabase myDatabase;
private final Context myContext;
public POIHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDatabase() throws IOException {
boolean dbExist = checkDatabase();
if(dbExist) {
} else {
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDatabase() {
SQLiteDatabase checkDb = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if(checkDb != null) {
checkDb.close();
}
return checkDb != null ? true : false;
}
private void copyDatabase() throws IOException{
InputStream input = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
}
public void openDatabase() throws IOException {
String path = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDatabase != null)
myDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}}
错误日志:
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): FATAL EXCEPTION: main
01-10 16:11:52.332: ERROR/AndroidRuntime(1311):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.test.mysgapp/com.test.mysgapp.POITab}:
android.database.sqlite.SQLiteException: no such table: Places: ,
while compiling: SELECT _id, Name FROM Places 01-10 16:11:52.332:
ERROR/AndroidRuntime(1311): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.widget.TabHost.setCurrentTab(TabHost.java:323) 01-10
16:11:52.332: ERROR/AndroidRuntime(1311): at
android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129) 01-10
16:11:52.332: ERROR/AndroidRuntime(1311): at
android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.view.View.performClick(View.java:2408) 01-10 16:11:52.332:
ERROR/AndroidRuntime(1311): at
android.view.View$PerformClick.run(View.java:8816) 01-10 16:11:52.332:
ERROR/AndroidRuntime(1311): at
android.os.Handler.handleCallback(Handler.java:587) 01-10
16:11:52.332: ERROR/AndroidRuntime(1311): at
android.os.Handler.dispatchMessage(Handler.java:92) 01-10
16:11:52.332: ERROR/AndroidRuntime(1311): at
android.os.Looper.loop(Looper.java:123) 01-10 16:11:52.332:
ERROR/AndroidRuntime(1311): at
android.app.ActivityThread.main(ActivityThread.java:4627) 01-10
16:11:52.332: ERROR/AndroidRuntime(1311): at
java.lang.reflect.Method.invokeNative(Native Method) 01-10
16:11:52.332: ERROR/AndroidRuntime(1311): at
java.lang.reflect.Method.invoke(Method.java:521) 01-10 16:11:52.332:
ERROR/AndroidRuntime(1311): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-10
16:11:52.332: ERROR/AndroidRuntime(1311): at
dalvik.system.NativeStart.main(Native Method) 01-10 16:11:52.332:
ERROR/AndroidRuntime(1311): Caused by:
android.database.sqlite.SQLiteException: no such table: Places: ,
while compiling: SELECT _id, Name FROM Places 01-10 16:11:52.332:
ERROR/AndroidRuntime(1311): at
android.database.sqlite.SQLiteCompiledSql.native_compile(Native
Method) 01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:64)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:46) 01-10
16:11:52.332: ERROR/AndroidRuntime(1311): at
android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
com.test.mysgapp.POITab.openDb(POITab.java:42) 01-10 16:11:52.332:
ERROR/AndroidRuntime(1311): at
com.test.mysgapp.POITab.onCreate(POITab.java:36) 01-10 16:11:52.332:
ERROR/AndroidRuntime(1311): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-10 16:11:52.332: ERROR/AndroidRuntime(1311): ... 18 more 01-10
16:11:52.392: WARN/ActivityManager(41): Force finishing activity
com.test.mysgapp/.MySgApp