I have implemented an application with Activty,Service,SQLiteOpenHelper.In my application I am getting the data from service class and send to SQLiteOpenHelper class,this class is saving the data in to SQlite db and I am retriving the data from This SQLiteOpenHelper class to Activity class by using SimpleCurserAdapeter then updating to ListView.
I have used code structure as follows:
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
}
}
}
From the above code when i am exit from my application i am getting an error as:
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
How can i Resolve the above error? and the timer is not cancelling…
In the
onDestroy()method you first close the database then try to delete records(in the activity and in the service as well). Try this:Edit:
Another source of your problem could be that in the
onDestroy()method of yourActivityMyActivity.java you first call :that calls the service’s
onDestroy()method. In that method you delete the records and then close the database. After this your return to the activity’sonDestroy()method where again try to delete the records(but the database is closed from the service) and then again close the database.In the activity
onDestroytry to first check if the database is still open before attempting to delete all records: