我已經用Activty,Service,SQLiteOpenHelper實現了一個應用程序。在我的應用程序中,我從服務類中獲取數據併發送到SQLiteOpenHelper類,該類將數據保存到SQlite數據庫中,並且正在從此SQLiteOpenHelper類通過使用SimpleCurserAdapeter然後更新到ListView的Activity類。 我已經使用的碼結構,如下所示:當我使用SQliteDbHelper時如何解決數據庫未打開錯誤?
MyActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testlist);
IntentFilter filter = new IntentFilter("com.sample.presentationLayer");
registerReceiver(myReceiver, filter);
startService(new Intent(MyActivity.this, RepeatService.class));
}
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
objSqlite= new MySqliteHelper(MyActivity.this);
objSqlite.openToWrite();
lst = ((ListView)findViewById(R.id.listView1));
cursor = objSqlite.queueAll();
getFromDB = new String[]{MySqliteHelper.KEY_CONTENT2,MySqliteHelper.KEY_CONTENT3};
toView = new int[]{R.id.usrName,R.id.msgText};
lst.setAdapter(new SimpleCursorAdapter(NewShoutGetMessagesScrn.this, R.layout.test, cursor, getFromDB, toView));
updateList();
}
};
@Override
protected void onDestroy() {
super.onDestroy();
stopService(new Intent(MyActivity.this, RepeatService.class));
unregisterReceiver(myReceiver);
if(objSqlite.isOpen) {
objSqlite.deleteAll();
objSqlite.close();
}
}
RepeatService.java:
private Timer timer = new Timer();
private static final long UPDATE_INTERVAL = 20000;
private MySqliteHelper objSqlite;
@Override
public void onCreate() {
objSqlite = new MySqliteHelper(RepeatService.this);
objSqlite.openToWrite();
pollForUpdates();
super.onCreate();
}
private void pollForUpdates() {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
for(int i=0;i<result.size();i++)
{
String userID = result.get(i).getUserID();
String userName = result.get(i).getName();
String userMessage=result.get(i).getMessageText();
objSqlite.insert(userID, userName, userMessage);
Intent intent = new Intent();
intent.setAction("com.sample.presentationLayer");
sendBroadcast(intent); // finally broadcast
}
}, 0, UPDATE_INTERVAL);
}
@Override
public void onDestroy() {
timer.cancel();
if(objSqlite.isOpen) {
objSqlite.deleteAll();
objSqlite.close();
}
}
MySqliteHelper.java
private Context context;
public MySqliteHelper(Context c){
context = c;
}
public MySqliteHelper openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public MySqliteHelper openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content1, String content2,String content3){
ContentValues contentValues = new ContentValues();
contentValues.put(USER_ID, content1);
contentValues.put(USER_NAME, content2);
contentValues.put(USER_MESSAGE, content3);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, USER_ID, USER_NAME, USER_MESSAGE};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, "_id DESC");
cursor.moveToFirst();
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
從上面的代碼,當我從我的應用我收到一個錯誤退出:
03-01 16:11:47.468: ERROR/AndroidRuntime(3038): Uncaught handler: thread main exiting due to uncaught exception
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): java.lang.RuntimeException: Unable to destroy activity {com.sample.presentationLayer/com.sample.presentationLayer.MyActivity}: java.lang.IllegalStateException: database not open
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3364)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3382)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at android.app.ActivityThread.access$2700(ActivityThread.java:116)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1826)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at android.os.Looper.loop(Looper.java:123)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at android.app.ActivityThread.main(ActivityThread.java:4203)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at java.lang.reflect.Method.invoke(Method.java:521)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at dalvik.system.NativeStart.main(Native Method)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): Caused by: java.lang.IllegalStateException: database not open
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1348)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at com.fitzgerald.shoutdataLayer.MySqliteHelper.deleteAll(MySqliteHelper.java:65)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at com.fitzgerald.presentationLayer.NewShoutGetMessagesScrn.onDestroy(MyActivity.java:58)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3351)
03-01 16:11:47.478: ERROR/AndroidRuntime(3038): ... 11 more
如何解決上面的錯誤?並且定時器不取消...
即使它正在發生同樣的事情 – 2012-03-01 10:58:09
@ prasad.gai你做的活動'MyActivity的變化。 java'和在服務'RepeatService'? – Luksprog 2012-03-01 11:01:06
是的,請找到我的更新代碼 – 2012-03-01 11:04:36