我想使用我使用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):致命异常:主要 01-10 16:11:52.332:ERROR / AndroidRuntime(1311): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.test.mysgapp / com.test.mysgapp.POITab}: android.database.sqlite.SQLiteException:没有这样的表:Places :, 编译时:SELECT _id,Name FROM Places 01-10 16:11:52.332: 错误/ 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: 错误/ AndroidRuntime(1311):at android.view.View $ PerformClick.run(View.java:8816)01-10 16:11:52.332: 错误/ 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: 错误/ 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: 错误/ 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):引起: android.database.sqlite.SQLiteException:没有这样的表:Places :, 编译时:SELECT _id,Name FROM Places 01-10 16:11:52.332: 错误/ AndroidRuntime(1311):at android.database.sqlite.SQLiteCompiledSql.native_compile(母语 方法)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: 错误/ AndroidRuntime(1311):at com.test.mysgapp.POITab.onCreate(POITab.java:36)01-10 16:11:52.332: 错误/ 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更多01-10 16:11:52.392:WARN / ActivityManager(41):强制完成活动 com.test.mysgapp / .MySgApp
答案 0 :(得分:0)
我猜测但是不应该将setListAdapter(适配器)添加到列表中? 像myList.setListAdapter(adapter);
答案 1 :(得分:0)
感谢观看和帮助。我设法解决了这个问题。它位于数据库中。数据库文件有问题。我创建了一个新的,现在一切正常。