android - 游标索引越界异常 0 android

标签 android database sqlite

我开发了一个 android 应用程序,它有一本小书的内容。该应用程序有一个“继续阅读”选项,当“按下 onback”和“暂停”时使用,它存储页面和共享首选项的其他信息并在按下继续阅读时检索它们...但是它显示“光标索引超出范围异常”使用此选项时请求的索引 0 大小为 0 并强制关闭应用程序。我希望它至少保留一段时间... 这些代码在其他应用程序中工作正常,但在这里显示异常

这里是澄清问题的方法:

1 : 通常在设备上安装apk文件并打开文本阅读。

2:用户按下后退按钮并转到应用程序的其他部分,例如设置或...

3:用户在关闭应用程序前按下“继续阅读”应用程序可以检索他正在阅读的内容。

4:用户关闭应用。

5:再次打开并按“继续阅读”

6:应用无法检索文本并强制关闭。

或:

1: 在模拟器中从 eclipse 运行应用程序。

2:只要代码打开并从 eclipse 运行,它就可以正常工作。

3:关闭模拟器或断开与eclips的连接。

4:运行模拟器

5:打开应用程序并按“继续阅读”。

6:强制关闭

这是我的“TextShow”类:

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();
}

这是我的主类:

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();
            }


        }

    });

这是我的数据库类...检索文本的方法:

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;

}

最佳答案

添加检查以查看光标是否有项目:

if (cu.moveToFirst()) {
    String s=cu.getString(4);
} else {
    Log.e("TAG", "Cursor empty!");
}

关于android - 游标索引越界异常 0 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27999939/

相关文章:

java - socket.outputStream 为什么要花这么多时间?

android - 使用来自多个模块 dagger 2.11 android 的依赖项

android - ViewPager 和数据库

java - SQL 查询的准备语句,错误 DB2 SQL 错误 : SQLCODE=-206, SQLSTATE=42703

python - 合并两个数据框 - python 中的 UPSERT

c - 读取 SQLite header

java - collectionGroup 文档中的数据未加载到 TextView Firestore 中

c# - 适用于小型企业应用程序的后端或数据库系统

java - 每当我向数据库添加一些表时,就没有这样的表错误

android - PowerVR SGX540 Android 纹理问题