0

它显示 70% 的记录,但其他 30% 的记录未能显示并出现错误:

由于错误 -12,无法分配大小为 4194304 的 CursorWindow '/data/data/com.backapp_inter/databases/backapp'。”

这是我的代码:

package com.backapp_inter.db;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class dbquery {
ContentResolver cr;
Context c;
DatabaseHelper dh;
SQLiteDatabase db;
    public dbquery(ContentResolver cr,Context c) {
    // TODO Auto-generated constructor stub
        this.cr=cr;
        this.c=c;
        dh=new DatabaseHelper(c);
        db=dh.getWritableDatabase();
}

这是列出联系人姓名、其电子邮件和号码的逻辑。

    public void process(){


        Cursor name = db.query("name", new String[]{"ID","name"}, null, null, null, null, null);
        if(name.moveToFirst()){
            Log.e("Total Name",""+name.getCount());

            while(!name.isAfterLast()) {    
                Log.e("Name",name.getString(1));
                givemeemail(name.getInt(name.getColumnIndex("ID")));
        //      givemephone(name.getInt(name.getColumnIndex("ID")));
                name.moveToNext();
            }
        }

    name=null;
    }
    public void givemeemail(int i) {
        // TODO Auto-generated method stub
        try{
            Log.v("TRACK #00","givemeemail");
            Cursor email = db.query("email", new String[]{"edata","etype"}, "fID=?", new String[]{String.valueOf(i)},null, null, null); 
//          Log.e("Edata",email.getColumnIndex("edata")+"");
//          Log.e("Etype",email.getColumnIndex("etype")+"");
            if(email.moveToFirst()){
                while(!email.isAfterLast()) {   
                    Log.e("Email",email.getString(0));
                    Log.e("Email Type",email.getString(1));

                    email.moveToNext();
                }

            }
            email=null;
        }
        catch(Exception e){
            if(e!=null){
                Log.v("Number Parse Exception","dbquery.java");
            }
        }

    }
    public void givemephone(int i) {
        // TODO Auto-generated method stub
        try{
            Log.v("TRACK #00","givemephone");
            Cursor phone = db.query("phone", new String[]{"pdata","ptype"}, "fID=?", new String[]{String.valueOf(i)},null, null, null); 

            if(phone.moveToFirst()){
                while(!phone.isAfterLast()) {   
                    Log.e("Phone",phone.getString(0));
                    Log.e("Phone Type",phone.getString(1));

                    phone.moveToNext();
                }
            }
        phone=null;
        }
        catch(Exception e){
            if(e!=null){
                Log.v("Number Parse Exception","dbquery.java");
            }
        }

    }
}


Logcat :

01-24 17:09:41.601: E/Name(32656): nilu_160488@yahoo.com
01-24 17:09:41.601: V/TRACK #00(32656): givemeemail
01-24 17:09:41.601: E/Email(32656): nilu_160488@yahoo.com
01-24 17:09:41.601: E/Email Type(32656): Home
01-24 17:09:41.601: E/Name(32656): scope4it
01-24 17:09:41.601: V/TRACK #00(32656): givemeemail
01-24 17:09:41.601: E/CursorWindow(32656): Could not allocate CursorWindow '/data/data/com.backapp_inter/databases/backapp' of size 4194304 due to error -12.
01-24 17:09:41.611: V/Number Parse Exception(32656): Email + dbquery.java
01-24 17:09:41.611: E/Name(32656): Suthar Ila
01-24 17:09:41.611: V/TRACK #00(32656): givemeemail
01-24 17:09:41.611: E/CursorWindow(32656): Could not allocate CursorWindow '/data/data/com.backapp_inter/databases/backapp' of size 4194304 due to error -12.
01-24 17:09:41.611: V/Number Parse Exception(32656): Email + dbquery.java
01-24 17:09:41.611: E/Name(32656): Chaitanya Prajapati
01-24 17:09:41.611: V/TRACK #00(32656): givemeemail
01-24 17:09:41.611: E/CursorWindow(32656): Could not allocate CursorWindow '/data/data/com.backapp_inter/databases/backapp' of size 4194304 due to error -12.
01-24 17:09:41.611: V/Number Parse Exception(32656): Email + dbquery.java
01-24 17:09:41.611: E/Name(32656): piadolandia-unsubscribe@googlegroups.com
01-24 17:09:41.611: V/TRACK #00(32656): givemeemail
01-24 17:09:41.611: E/CursorWindow(32656): Could not allocate CursorWindow '/data/data/com.backapp_inter/databases/backapp' of size 4194304 due to error -12.
01-24 17:09:41.611: V/Number Parse Exception(32656): Email + dbquery.java
01-24 17:09:41.611: E/Name(32656): Ahmed_recruiter+unsubscribe@googlegroups.com
01-24 17:09:41.611: V/TRACK #00(32656): givemeemail
01-24 17:09:41.611: E/CursorWindow(32656): Could not allocate CursorWindow '/data/data/com.backapp_inter/databases/backapp' of size 4194304 due to error -12.
01-24 17:09:41.611: V/Number Parse Exception(32656): Email + dbquery.java
4

1 回答 1

2

您应该始终确保关闭光标,最好在 finally 块中,仅将引用设置为 null 不会释放它的资源。

...
Cursor cursor = null;
try {
 ..open your cursor, run query, loop etc
} finally {
 if (cursor!=null) cursor.close();
}
于 2013-01-24T12:36:59.717 回答