-1

The app crashes whenever it reaches the line where cursor.getString() is executed.

I've tried both cursor.getString() and cursor.getString(cursor.getColumnIndex(" ")) but it still crashed

The method it's executed on:

public void populateView(){
    Cursor data = myDb.getData();
    data.moveToFirst();
    String title = data.getString(data.getColumnIndex(1));
    newsList.add(new NewsItem(R.drawable.ic_home_black_24dp, title, "Description"));
    mAdapter = new NewsItemAdapter(newsList);
    mRecyclerView.setAdapter(mAdapter);
}

The method in DatabaseHelper class:

public Cursor getData(){
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT * FROM " + TABLE_NAME_REPORT;
    Cursor data = db.rawQuery(query, null);

    data.close();
    db.close();

    return data;
}

P.S. The NewsItem class is accepting these arguments:

public NewsItem(int imageResource, String title, String desc){
    mImageResource = imageResource;
    mTitle = title;
    mDescription = desc;
}

Process: com.example.reportingapplication, PID: 32448
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM report_data
    at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
    at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:151)
    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:139)
    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:220)
    at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:265)
    at com.example.reportingapplication.Fragments.HomeFragment.populateView(HomeFragment.java:104)
    at com.example.reportingapplication.Fragments.HomeFragment$1.onClick(HomeFragment.java:58)
    at android.view.View.performClick(View.java:6608)
    at android.view.View.performClickInternal(View.java:6585)
    at android.view.View.access$3100(View.java:785)
    at android.view.View$PerformClick.run(View.java:25921)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6810)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
OJDylan
  • 27
  • 7
  • What's the Exception thrown? – fjc Jul 10 '19 at 13:24
  • What does "crash the app" mean? Please edit the question and post a full stack trace. There is no variable called "cursor" in your example. Post a [mcve]. – OldProgrammer Jul 10 '19 at 13:25
  • 5
    Please include any error messages or stack traces. It's not always feasible to figure out what's going on just by looking at the code. However, in this case the problem _appears_ to be the fact that you're closing your cursor before you use it. From the [relevant docs](https://developer.android.com/reference/android/database/Cursor): "**close() -- Closes the Cursor, releasing all of its resources and making it completely invalid.**" – Jordan Jul 10 '19 at 13:27
  • 3
    `java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM report_data` ← there's your problem, as @Jordan suspected. – Slaw Jul 10 '19 at 13:30
  • holy moly @Jordan, thank you! – OJDylan Jul 10 '19 at 13:32

1 Answers1

0

Problem was closing the cursor before it was even used. Thanks to @Jordan who pointed it out in the comments.

OJDylan
  • 27
  • 7