Reputation: 11
I have developed an android application which has content of a little book.. the application have an option of " continue reading" which used when " onback pressed" and " on pause" it stores the page and other info on shared preferences and retrieve them when pressing continue reading... however it shows" cursor index out of bounds exception" index 0 requested with size of 0 when using this option and force closes the app. i want it to remain for a while at least... the codes are working fine in other application but here it shows exception
here is what happens to clarify the problem:
1 : normally install the apk file on device and open a text to read.
2: user press back button and goes to other parts of the application like setting or....
3: user press " continue reading" BEFORE CLOSING THE APP" app can retrieve what ever he was reading.
4: user closes the app.
5: opens it again and press "continue reading"
6:app cant retrieve the text and force closes.
or:
1:run the app from eclipse in emulator .
2:it does everything ok as long as the code is opene and run from eclipse.
3:shutdown the emulator or disconnect from eclips.
4:run the emulator.
5:open the app and press "continue reading".
6:force close.
here is my "TextShow" class:
protected void onCreate(Bundle savedInstanceState) {
load(book,name,Page2);
}
private void load(String sea,String Name,int page3){
db.open();
titr.setText(Name);
matn.setText(db.main_display("content", sea, Name, page3));
page.setText("page"+ Page2 + "from"+ Page1);
db.close();
}
public void onBackPressed() {
SharedPreferences sp=getApplicationContext().getSharedPreferences("cont", 0);
Editor ed=sp.edit();
ed.clear();
ed.putString("book", book);
ed.putString("name", name);
ed.putInt("page",Page1);
ed.commit();
finish();
}
@Override
protected void onPause() {
SharedPreference sp=getApplicationContext().getSharedPreferences("cont", 0);
Editor ed=sp.edit();
ed.clear();
ed.putString("book", book);
ed.putString("name", name);
ed.putInt("page",Page1);
ed.commit();
super.onPause();
}
here is my Main class:
ImageView cont=(ImageView) findViewById(R.id.cont);
cont.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
sp=getApplicationContext().getSharedPreferences("cont", 0);
if(sp.getString("name", "*")!="*"){
Intent i=new Intent(Main.this,TextShown.class);
i.putExtra("book",sp.getString("book", "*"));
i.putExtra("name",sp.getString("name", "*"));
i.putExtra("page",sp.getInt("page", 0)+"");
startActivity(i);
overridePendingTransition(R.anim.in, R.anim.out);
}else{
Toast.makeText(getApplicationContext(), "there is no ending point", Toast.LENGTH_LONG).show();
}
}
});
here is my database class...method for retrieving the text :
public String main_display(String table,String sea,String name,int page){
Cursor cu=mydb.rawQuery("select * from "+table+" where BookName='"+sea+"' and StoryName='"+name+"' and Page="+page, null);
cu.moveToFirst();
String s=cu.getString(4);
return s;
}
Upvotes: 1
Views: 257
Reputation: 709
The exception says, that the data at index 4 could not be found, because it is not a valid index. Please note, that indexes starts from 0.
Here are some general improvements to avoid suche mistakes:
1.) You should not use hard coded indexes when retrieving data from cursor, because you can easily messed up them. Instead of that you should create a class, which simply holds the column names as string constants. Then you can define your table as follows:
private static final String CREATE_TABLE_LOGIN = "CREATE TABLE IF NOT EXISTS "
+ LoginColumns.TABLE_NAME + "(" + LoginColumns._ID + " INTEGER PRIMARY KEY autoincrement, "
+ LoginColumns.KEY_LOGIN_ABCOCOE + " VARCHAR(100) NOT NULL, "
+ LoginColumns.KEY_LOGIN_VALID + " INTEGER NOT NULL);";
2.) You should always add a check to see if the cursor has items and use the column names instead of the plain index.
public String main_display(String table,String sea,String name,int page){
Cursor cu=mydb.rawQuery("select * from "+table+" where BookName='"+sea+"' and StoryName='"+name+"' and Page="+page, null);
if (cu.moveToFirst()){
// just an example
String s=cu.getString(cursor.getColumnIndex(LoginColumns.KEY_LOGIN_ABCOCOE)
return s;
}
return null;
}
If this does not fix your problem, please provide some more information such as stacktrace etc.
Upvotes: 1
Reputation: 44188
Add a check to see if cursor has items:
if (cu.moveToFirst()) {
String s=cu.getString(4);
} else {
Log.e("TAG", "Cursor empty!");
}
Upvotes: 1