0
我正在用SQLite數據庫做一個「選擇你自己的結局」的故事,但是我遇到了一個錯誤,它說我的數據庫是空的或者什麼時候試圖打開連接。
任何人都可以啓發我爲什麼發生這種情況?getReadableDatabase爲什麼返回null?
主要活動:
public class StoryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState != null) {
story = savedInstanceState.getParcelable("story");
} else {
story = new Story(this);
story.getStory(1);
}
}
@Override
public void onSaveInstanceState(Bundle state) {
state.putParcelable("story", story);
}
}
故事類別:
public class Story implements Parcelable {
private final String DATABASE_NAME = "story";
private final int DATABASE_VERSION = 1;
private int id, destinationA, destinationB;
private String txtStory, decisionA, decisionB;
private Cursor cursor;
private SQLiteDatabase db;
private SQLiteOpenHelper dbHelper;
private Context context;
public Story(Context con) {
id = -1;
txtStory = "";
decisionA = "";
decisionB = "";
destinationA = -1;
destinationB = -1;
cursor = null;
db = null;
dbHelper = null;
bundleDB(con);
}
private void bundleDB(Context con) {
try {
String destPath = "/data/data/" + con.getPackageName() + "/databases/" + DATABASE_NAME;
File f = new File(destPath);
if (!f.exists()) {
Log.d("TEST","Copying database");
File directory = new File("/data/data/" + con.getPackageName() + "/databases");
directory.mkdir();
copyDB(con.getAssets().open(DATABASE_NAME), new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
Log.d("TEST", "FileNotFoundException: " + e.toString());
} catch (IOException e) {
Log.d("TEST", "IOException: " + e.toString());
}
}
private void copyDB(InputStream i, OutputStream o) throws IOException {
byte[] buffer = new byte[1024];
int length;
length = i.read(buffer);
while (length > 0) {
o.write(buffer, 0, length);
length = i.read(buffer);
}
i.close();
o.close();
Log.d("TEST", "Database has been bundled with app");
}
public Boolean getStory(int i) {
try {
open();
cursor = null;
if (db == null) return false;
cursor = db.rawQuery("SELECT * FROM tblStory WHERE id = ?", new String[]{Integer.toString(i)});
if (cursor.getCount() == 0) return false;
cursor.moveToFirst();
id = cursor.getInt(0);
txtStory = cursor.getString(1);
decisionA = cursor.getString(2);
decisionB = cursor.getString(3);
destinationA = cursor.getInt(4);
destinationB = cursor.getInt(5);
close();
} catch (Exception e) {
Log.d("TEST", "getStory exception: " + e.getMessage());
}
return true;
}
public void open() {
dbHelper = new DBHelper(context);
db = dbHelper.getReadableDatabase(); // This is the line that is bugged
}
public void close() {
db.close();
}
// Inner class for use via open() method
private class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
// Parcelable methods
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
數據庫(位於資產文件夾):
tblStory (table)
id (integer primary key)
story (text)
decisionA (text)
decisionB (text)
destinationA (numeric)
destinationB (numeric)
logcat的:
Copying database
Database has been bundled with app
getStory exception: null
請發佈堆棧跟蹤。什麼是拋出的異常? – Emmanuel
在原始文章的底部,您可以看到第一次運行的LogCat。 –
這就是你的'Log'調用的輸出。我的意思是拋出'Exception'的實際logcat輸出。 – Emmanuel