Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:10:21+00:00 2026-05-30T16:10:21+00:00

I have implemented an application with Activty,Service,SQLiteOpenHelper.In my application I am getting the data

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:10:22+00:00Added an answer on May 30, 2026 at 4:10 pm

    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:

    objSqlite.deleteAll();
    objSqlite.close();
    

    Edit:
    Another source of your problem could be that in the onDestroy() method of your Activity MyActivity.java you first call :

    stopService(new Intent(MyActivity.this, RepeatService.class)); 
    

    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’s onDestroy() 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 onDestroy try to first check if the database is still open before attempting to delete all records:

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(MyActivity.this, RepeatService.class)); 
        unregisterReceiver(myReceiver);
        if(objSqlite.isOpen) { 
           objSqlite.deleteAll(); 
           objSqlite.close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET 3.5 web application for which I have implemented a class
I have written a service To delete data from a table when data inputed
I have implemented lucene for my application and it works very well unless you
We have implemented SSO in a .NET web application using Windows Identity Foundation (WIF).
I have a small application in C#, and so far I have implemented it
In our desktop application, we have implemented a simple search engine using an inverted
I have a VB6 application implemented as an ActiveX exe. I also have a
I have successfully implemented interop beftween Win32 application and managed .Net dll as described
I have successfully implemented SUBSONIC DAL in my desktop application. it was superb experience.
We have issues within an application using a state machine. The application is implemented

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.