java.lang.OutOfMemoryError:[内存耗尽]读取数据从Sqlite Android

我已经成功地保存我的数据在SQLite数据库。但我得到一个错误,而读取数据从我的SQLite数据库,然后我的应用程序崩溃。

错误消息

01-01 05:42:13.916 607-607/com.example.eyesaver I/dalvikvm:   | sysTid=607 nice=0 sched=0/0 cgrp=apps handle=68313184
01-01 05:42:13.916 607-607/com.example.eyesaver I/dalvikvm:   | sysTid=607 nice=0 sched=0/0 cgrp=apps handle=68313184
01-01 05:42:13.916 607-607/com.example.eyesaver I/dalvikvm:   | state=R schedstat=( 0 0 0 ) utm=2910 stm=2910 core=0
01-01 05:42:13.916 607-607/com.example.eyesaver I/dalvikvm:   | state=R schedstat=( 0 0 0 ) utm=2910 stm=2910 core=0
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm:     at android.database.CursorWindow.nativeGetString(Native Method)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm:     at android.database.CursorWindow.nativeGetString(Native Method)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm:     at android.database.CursorWindow.getString(CursorWindow.java:434)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm:     at android.database.CursorWindow.getString(CursorWindow.java:434)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm:     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm:     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm:     at com.example.eyesaver.SQ_Lite_DB.ReadSqliteData(SQ_Lite_DB.java:59)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm:     at com.example.eyesaver.SQ_Lite_DB.ReadSqliteData(SQ_Lite_DB.java:59)// I marked this line in code

1-01 05:42:16.189 607-607/com.example.eyesaver E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.eyesaver, PID: 607
    java.lang.OutOfMemoryError: [memory exhausted]
        at dalvik.system.NativeStart.main(Native Method)
01-01 05:42:16.189 607-607/com.example.eyesaver E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.eyesaver, PID: 607
    java.lang.OutOfMemoryError: [memory exhausted]
        at dalvik.system.NativeStart.main(Native Method)

我使用'ReadSqliteData()'方法从我的SQLite读取数据

ReadSqliteData()方法

public void ReadSqliteData(Context context){
    ArrayList<Model> list = new ArrayList<>();
    Adpter adpter = new Adpter(list,context);
    SQLiteDatabase database = getWritableDatabase();
    Cursor cursor = database.rawQuery("Select name, image from orders",null);
    if (cursor.moveToFirst()){
        while (cursor.moveToFirst()){
            Model model = new Model();
            model.setImage(cursor.getString(0));   //i am getting error here 
            model.setName(cursor.getString(1));    //i am getting error here 
            list.add(model);
        }
        adpter.notifyDataSetChanged();
    }
    cursor.close();
    database.close();
}

还有一个问题:我的id在SQLite数据库中是空的,我不知道为什么?我正在创建一个这样的表

@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(
                "Create Table orders"+
                        "(id INTERGER PRIMARY KEY,"+
                        "image text ,"+
                        "name text)"
        );
    }

如果你需要任何其他信息,请告诉我。

###你的循环永远不会结束,因为在每次迭代中,你设置游标的第一行的索引moveToFirst() without advancing to the next row.
不用走到下一排。< br / >

使用moveToNext() onl只有:

public void ReadSqliteData(Context context){
    ArrayList<Model> list = new ArrayList<>();
    Adpter adpter = new Adpter(list,context);
    SQLiteDatabase database = getWritableDatabase();
    Cursor cursor = database.rawQuery("Select name, image from orders",null);
    while (cursor.moveToNext()){
        Model model = new Model();
        model.setImage(cursor.getString(0));            
        model.setName(cursor.getString(1));    
        list.add(model);
    }
    adpter.notifyDataSetChanged();
    cursor.close();
    database.close();
}

###尝试使用下面的代码。

//update your table create query, id should be auto increment and also your query was not in correct form.
 db.execSQL(
            "Create Table orders"+
                    "("+ id INTEGER PRIMARY KEY AUTOINCREMENT,"+
                    "image text ,"+
                    "name text" +")"
    );
//update you read method code
 public void ReadSqliteData(Context context){
ArrayList<Model> list = new ArrayList<>();
Adpter adpter = new Adpter(list,context);
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.rawQuery("Select name, image from orders",null);
if (cursor.moveToNext()){
    while (cursor.moveToFirst()){
        Model model = new Model();
        model.setImage(cursor.getString(0));   
        model.setName(cursor.getString(1));   
        list.add(model);
    }
    adpter.notifyDataSetChanged();
}
cursor.close();
database.close();
}
阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码