我的主要课程
public class Tips2Main extends Activity{
DBOpener dbopener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbopener = new DBOpener(this);
}
@Override
protected void onResume() {
super.onResume();
// Configure the listview
ArrayList<String> tips = new ArrayList<String>();
ListView lstTips = (ListView)this.findViewById(R.id.lst_tips);
lstTips.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tips));
dbopener.createDatabase();
Cursor Tips = dbopener.getTips();
while (Tips.moveToNext()) {
tips.add(Tips.getString(0)); // Get the current subj
}
Tips.close();
lstTips.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tips));
// Update the listview
ArrayAdapter<String> ad = (ArrayAdapter<String>)lstTips.getAdapter();
ad.notifyDataSetChanged();
}
//Close the DB when app pauses
@Override
protected void onPause() {
super.onPause();
dbopener.close();
}
}
我的数据库:
public class DBOpener extends Activity{
private static String DB_PATH =
"/data/data/sg.edu.iit.mns/databases/"; //path of our database
private static String DB_NAME ="Tips"; // Database name
private final Context myContext;
private SQLiteDatabase db;
public DBOpener(Context context) {
//super(context, DB_NAME, null, 1 );
myContext = context;
}
public void createDatabase() {
boolean dbExists = checkDatabase();
if (!dbExists) {
try
{
copyDatabase();
}
catch (IOException e)
{
Toast.makeText(myContext, "Error", Toast.LENGTH_LONG).show();
}
}
String path = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
}
private boolean checkDatabase() {
File database = new File(DB_PATH + DB_NAME);
return database.exists();
}
private void copyDatabase() throws IOException {
InputStream istream = myContext.getAssets().open(DB_NAME);
OutputStream ostream = new FileOutputStream(DB_PATH + DB_NAME);
// Transfer bytes from istream to ostream
byte[] buffer = new byte[1024];
int length;
while ((length = istream.read(buffer)) > 0) {
ostream.write(buffer, 0, length);
}
// Close streams
istream.close();
ostream.flush();
ostream.close();
}
public synchronized void close()
{
if(db !=null)
db.close();
}
// Retrieve tips
public Cursor getTips() {
if (db == null)
return null;
return db.query("tips", new String[] {"Tips"},
null, null, null, null, null);
}
错误日志:
06-06 04:21:34.214: I/Database(719): sqlite returned: error code = 14, msg = cannot open file at source line 25467
06-06 04:21:34.224: E/Database(719): sqlite3_open_v2("/data/data/sg.edu.iit.mns/databases/Tips", &handle, 2, NULL) failed
06-06 04:21:34.224: D/AndroidRuntime(719): Shutting down VM
06-06 04:21:34.224: W/dalvikvm(719): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-06 04:21:34.264: E/AndroidRuntime(719): FATAL EXCEPTION: main
06-06 04:21:34.264: E/AndroidRuntime(719): java.lang.RuntimeException: Unable to resume activity {sg.edu.iit.mns/sg.edu.iit.mns.Tips2Main}: android.database.sqlite.SQLiteException: unable to open database file
06-06 04:21:34.264: E/AndroidRuntime(719): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.os.Looper.loop(Looper.java:123)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 04:21:34.264: E/AndroidRuntime(719): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 04:21:34.264: E/AndroidRuntime(719): at java.lang.reflect.Method.invoke(Method.java:521)
06-06 04:21:34.264: E/AndroidRuntime(719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 04:21:34.264: E/AndroidRuntime(719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 04:21:34.264: E/AndroidRuntime(719): at dalvik.system.NativeStart.main(Native Method)
06-06 04:21:34.264: E/AndroidRuntime(719): Caused by: android.database.sqlite.SQLiteException: unable to open database file
06-06 04:21:34.264: E/AndroidRuntime(719): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1812)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
06-06 04:21:34.264: E/AndroidRuntime(719): at sg.edu.iit.mns.DBOpener.createDatabase(DBOpener.java:41)
06-06 04:21:34.264: E/AndroidRuntime(719): at sg.edu.iit.mns.Tips2Main.onResume(Tips2Main.java:33)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.app.Activity.performResume(Activity.java:3823)
06-06 04:21:34.264: E/AndroidRuntime(719): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
06-06 04:21:34.264: E/AndroidRuntime(719): ... 12 more
当我尝试从数据库中填充我的列表视图时,它会强制关闭应用程序。我不确定如何进行错误检查并使其运行。这里没什么帮助。谢谢。数据库已经在资产文件夹中。