-2

Hello I am trying to query data from local database in android. There are two models Category and News in 1:m relationship. I want to search for list of news according to their categoryID. But when I do it I am getting following exception.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infiniteloop.newsmobile/com.infiniteloop.newsmobile.NewsList}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.infiniteloop.newsmobile/databases/NEWS_DB
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                           at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                        Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.infiniteloop.newsmobile/databases/NEWS_DB
                                                                           at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
                                                                           at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1312)
                                                                           at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
                                                                           at com.infiniteloop.newsmobile.db.NewsDBAdapter.getNewsByCategory(NewsDBAdapter.java:105)
                                                                           at com.infiniteloop.newsmobile.data_models.NewsManager.getNewsByCategory(NewsManager.java:100)

This is my query in NewsDBAdapter.

public Cursor getNewsByCategory(int Cat_ID){

    Cursor  c = db.rawQuery("SELECT * FROM " + TABLE_NEWS + " WHERE " +
            NEWS_CATEGORY_ID + " = " + Cat_ID + ";", null);

    return c;
}

I am calling this method in another class

public List<News> getNewsByCategory(int categoryID){

    List<News> newsList = new ArrayList<>();
    Cursor c = null;
    try {

        if(!adapter.isDBOpen()){
            adapter.openDB();
        }
        c = adapter.getNewsByCategory(categoryID);
        Log.i(TAG, "getNewsByCategory: " +  c);
        if (c.moveToFirst()) {
            do {
                int News_ID = c.getInt(c.getColumnIndex(NEWS_ID));
                String News_TITLE = c.getString(c.getColumnIndex(NEWS_TITLE));
                String News_Content = c.getString(c.getColumnIndex(NEWS_CONTENT));
                String News_Date = c.getString(c.getColumnIndex(NEWS_DATE));
                String News_IMGURL = c.getString(c.getColumnIndex(NEWS_IMGURL));
                int News_Category_ID = c.getInt(c.getColumnIndex(NEWS_CATEGORY_ID));
                News news = new News(News_ID, News_TITLE, News_Content, News_Date, News_IMGURL, News_Category_ID);
                newsList.add(news);
            } while (c.moveToNext());
        }
        return newsList;
    }finally {
        if (c!=null){
            c.close();
        }
        if(adapter.isDBOpen()){
            adapter.closeDB();
        }
    }


}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

I think it is related to this post: Attempt to reopen an already-closed object sqlitedatabase

But you did not use the readable instance of the database. Try the below code:

public Cursor getNewsByCategory(int Cat_ID){
    SQLiteDatabase db = adapter.getReadableDatabase(); // You need to call
    Cursor  c = db.rawQuery("SELECT * FROM " + TABLE_NEWS + " WHERE " +
            NEWS_CATEGORY_ID + " = " + Cat_ID + ";", null);

    return c;
}
glagarto
  • 913
  • 8
  • 20