lundi 19 octobre 2015

IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it

I'm facing a problem while retrieving data from SQLite to editText. When the row is pressed, the app crashed. Does anyone know how to fix this?

error is launched here : public void onResume() { super.onResume();

    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    sortBy= sharedPrefs.getString(getString(R.string.pref_sort_key), getString(R.string.pref_most_popular));
    if (sortBy.equals(getString(R.string.pref_Favorite_movie))) {
        String Table_Name= MovieContract.MovieEntry.Table_name;
        String selectQuery = "SELECT*FROM " + Table_Name;
        MovieDBHelper MyHelperClass = new MovieDBHelper(getActivity());
        SQLiteDatabase db =  MyHelperClass.getReadableDatabase();
        MyHelperClass.onOpen(db);
        Cursor cursor = db.rawQuery(selectQuery, null);
        //String[] data = null;
        if (cursor.moveToFirst()) {
            do {
               // MovieGridView.setAdapter(MovieApdapter);


                String Image= cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry._POSTER));
                GridItem gridItemPoster = new GridItem(Image);
                GridItem.add(gridItemPoster);
                MovieGridView.setAdapter(MovieApdapter);

                String ID=cursor.getString(cursor.getColumnIndexOrThrow(MovieContract.MovieEntry._ID));
                MovieID.add(ID);


                String Title_Movie =cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry._NAME));
                TITLE_ARRAY.add(Title_Movie);

                String Release_Movie = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry._RELS));
                Release_ARRAY.add(Release_Movie);

                String Average = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry._AVG));
                Release_ARRAY.add(Average);

                String DESC = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry._DESC));
                OverView_ARRAY.add(DESC);


                prgs_Bar.setVisibility(View.GONE);

                MovieApdapter.clear();
                MovieApdapter.addAll(GridItem);

            } while (cursor.moveToNext());
        }
        db.close();
        cursor.close();
        MyHelperClass.close();

    }
    else {
        FetchAsyncTask fetchAsyncTask = new FetchAsyncTask();
        fetchAsyncTask.execute();

    }
}

And My Helper Class is :

 public void onCreate(SQLiteDatabase sqLiteDatabase) {
    final String Create_Table = "CREATE TABLE "
            + MovieContract.MovieEntry.Table_name +" ("
            + MovieContract.MovieEntry._ID + "INTEGER PRIMARY KEY,"
            + MovieContract.MovieEntry._NAME + " TEXT NOT NULL, "
            + MovieContract.MovieEntry._POSTER + " VARCHAR NOT NULL, "
            + MovieContract.MovieEntry._DESC + " TEXT NOT NULL, "
            + MovieContract.MovieEntry._AVG + " TEXT NOT NULL, "
            + MovieContract.MovieEntry._RELS + " TEXT NOT NULL);";
        sqLiteDatabase.execSQL(Create_Table);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + MovieContract.MovieEntry.Table_name);
    onCreate(sqLiteDatabase);
}

And Here's My LogCat :

10-19 20:53:31.120 8217-8217/com.example.rashaabuelmagd.popularmovie E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {com.example.rashaabuelmagd.popularmovie/com.example.rashaabuelmagd.popularmovie.MainActivity}: java.lang.IllegalArgumentException: column 'Movie_ID' does not exist

Aucun commentaire:

Enregistrer un commentaire