2013-04-09 79 views
1

雖然這是一個常見錯誤,並且可能會有這麼多與此異常相關的帖子。但這裏是奇怪的情況。我在Android 2.2中遇到了java.lang.IllegalStateException: database not open異常。在剩下的移動設備上運行良好。Gettine異常:java.lang.IllegalStateException:數據庫未打開

的logcat:

java.lang.IllegalStateException: database not open 
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1291) 
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1251) 
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1331) 
at com.mythrii.ilpa.DataHelper.checkRating(DataHelper.java:126) 
at com.mythrii.ilpa.SplashActivity$1.run(SplashActivity.java:35) 
at android.os.Handler.handleCallback(Handler.java:587) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:144) 
at android.app.ActivityThread.main(ActivityThread.java:4937) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
at dalvik.system.NativeStart.main(Native Method) 

我的代碼:

public class SplashActivity extends ActivityHelper 
{ 
private DataHelper dh; 
// Set the display time, in milliseconds (or extract it out as a configurable parameter) 
private final int SPLASH_DISPLAY_LENGTH = 3000; 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.act__splash); 

    dh = new DataHelper(this); 

    dh.inserORupdateRating(); 
} 

protected void onResume() 
{ 
    super.onResume(); 

     new Handler().postDelayed(new Runnable() 
     { 
      public void run() 
      { 
       clearPref(); 
       PrefBoolEdit("rating",dh.checkRating());//=> Here is the exception 
       SplashActivity.this.finish(); 
       Intent mainIntent = new Intent(SplashActivity.this, HomeActivity.class); 
       SplashActivity.this.startActivity(mainIntent); 
      } 
     }, SPLASH_DISPLAY_LENGTH); 

} 

protected void onDestroy() 
{ 
    super.onDestroy(); 
    if (dh != null) 
    { 
     dh.close(); 
    } 
} 
} 

我使用此構造在每一個活動來調用

Datahelper

public DataHelper(Context context) 
    { 

     this.context = context; 
     openHelper = new OpenHelper(this.context); 
     this.db  = openHelper.getWritableDatabase(); 

     Calendar c = Calendar.getInstance();  
     formattedDate = df.format(c.getTime()); 
    } 

CheckRating

public boolean checkRating() 
    { 
     Cursor cursor = this.db.query(TABLE_NAME_1, new String[] { "option" }, 
       null, null, null, null, null); 

     String option = null; 
     //String date = null; 

     boolean bool = false; 

     if (cursor.moveToFirst()) 
      { 
       do { 
        //date  = cursor.getString(0); 
        option = cursor.getString(0); 

       } while (cursor.moveToNext()); 
      } 
      if (cursor != null && !cursor.isClosed()) { 
       cursor.close(); 
      } 

      if(getRateCount()>0) 
      { 
       if(!option.equals(ALREADY_RATED)) 
       { 
        bool = true; 
       } 
       else 
       { 
        bool = false; 
       } 
      } 
      else 
      { 
       bool = false; 
      } 
     return bool; 
    } 

任何人可以幫助我,請..

+1

你在哪裏叫dh.open()? – 2013-04-09 04:32:10

+0

PLZ也加上checkRating方法代碼,問題 – 2013-04-09 04:33:39

+0

@HoanNguyen,請查看我編輯的問題 – Naveen 2013-04-09 04:36:11

回答

3

之前執行你應該打開數據庫的任何操作

dh.open(); 

See this onealso this one

+1

其實我是用usb作爲'dh = new datahelper(this)'來構造函數。那麼我在哪裏打電話dh.open(); – Naveen 2013-04-09 04:41:27

+0

我不知道如何寫你的dbhelper類代碼..但sholud有問題在上下文傳遞.. – QuokMoon 2013-04-09 04:45:00

0

這或許可以幫助你創建SQLiteOpenHelper

package org.groundme.sqlite; 

import org.groundme.general.General; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class TaskDatabaseHelper extends SQLiteOpenHelper { 

public static final String TAG = "TaskDatabaseHelper"; 
public static final String DATABASE_NAME = "MyTask.db"; 
public static final int DATABASE_VERSION = 9; 

public static final String TABLE_NAME = "tasklist"; 
public static String ID="id"; 
public static final String TASK_TITLE = "task_title"; 
public static final String TASK_DESC = "task_desc"; 
public static final String TASK_DATE = "task_date"; 
public static final String TASK_TIME = "task_time"; 
public static final String TASK_LOCATION = "task_location"; 
public static final String TASK_LAT = "task_lat"; 
public static final String TASK_LNG = "task_lng"; 
public static final String TASK_STATUS = "status"; 
public static final String TASK_TLBASE = "timeloc_base"; 
public static final String TASK_DURATION = "task_duration"; 


private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + 
    ID + " integer primary key autoincrement ," + 
    TASK_TITLE + " text," + 
    TASK_DESC + " text," + 
    TASK_DATE + " text," + 
    TASK_TIME + " text," + 
    TASK_LOCATION + " text ," + 
    TASK_LAT + " text ," + 
    TASK_LNG + " text ," + 
    TASK_STATUS + " text ," + 
    TASK_TLBASE + " INTEGER default 0," + 
    TASK_DURATION + " text)"; 

public TaskDatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    // TODO Auto-generated constructor stub 
    Log.i(General.TAG, TAG + "constructer"); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // TODO Auto-generated method stub 

    Log.i(General.TAG, TAG + "oncreate"); 
    db.execSQL(DATABASE_CREATE); 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    onCreate(db); 

}   

} 

以及公開課,並可以訪問它可能會幫助你。

public class TaskData { 
public TaskData(Context ctx) { 
    Log.i(General.TAG, TAG + "TaskData"); 
    taskdbhelper = new TaskDatabaseHelper(ctx); 

}

public void open() throws SQLException { 

    Log.i(General.TAG, TAG + "open"); 
    if (sqlitedb == null) { 
    sqlitedb = taskdbhelper.getWritableDatabase(); 
    } 
} 

}

通過創建TaskDataobject您可以創建PASE你的背景,在這裏你還可以更新,插圖,刪除功能通爲進一步使用。

相關問題