2013-12-09 25 views
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 
+0

请发布堆栈跟踪。什么是抛出的异常? – Emmanuel

+0

在原始文章的底部,您可以看到第一次运行的LogCat。 –

+1

这就是你的'Log'调用的输出。我的意思是抛出'Exception'的实际logcat输出。 – Emmanuel

回答

3

您可能忘记在Story中设置上下文。

在你的故事的构造只需添加

this.context = con; 

+0

哇,我不认为这会是一个简单的问题。谢谢一堆! –