尝试从数据库获取数据时内存耗尽 (android)

memory exhausted when trying to get data from database (android)

我正在尝试从我的数据库中获取一些信息。我是 android 的初学者。 我有一个名为 "Database" 的数据库创建 class 和一个名为 "Database_Acesso" 的数据库访问 class。他们看起来像这样:

Database.java: 包裹 workshopee.ct.ufrn.br.ssmonitor;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Database extends SQLiteOpenHelper{
    private static final int versao_db = 1;

    private static final String nome_db = "ssmonitor_db";

    private static final String table1 = "phone";

    private static final String id = "_id";
    private static final String longitude = "longitude";
    private static final String latitude = "latitude";
    private static final String forca_torres = "qtdtorres";
    private static final String forca_dbm = "dbm";
    private static final String mcc = "mcc";
    private static final String mnc = "mnc";
    private static final String phone_type = "phone_type";
    private static final String operadora = "operadora";
    private static final String network_type = "networkType";
    private static final String cid = "cid";
    private static final String lac = "lac";

    public Database(Context context) {
        super(context, nome_db, null, versao_db);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String criarTabela = "CREATE TABLE " + table1 + "("
                + id + " INTEGER PRIMARY KEY AUTOINCREMENT," + longitude + " REAL,"
                + latitude + " REAL," + forca_torres + " INTEGER," + forca_dbm + " REAL," + mcc + " INTEGER,"
                + mnc + " INTEGER," + phone_type + " TEXT," + operadora + " TEXT," + network_type + " INTEGER," + cid + " INTEGER,"
                + lac + " INTEGER )";
        db.execSQL(criarTabela);

               }
    @Override
    public void onUpgrade(SQLiteDatabase db, int versao_ant, int versao_nv) {
        Log.w(Database.class.getName(),
                "Atualizando o banco de dados da versão " + versao_ant + " para "
                        + versao_nv + ", isso apagará os dados antigos.");
        db.execSQL("DROP TABLE IF EXISTS " + table1 + ";");
        onCreate(db);

    }

    public void clear (SQLiteDatabase db) {
        Log.w(Database.class.getName(),
                "Apagando informações salvas anteriormente.");
        db.execSQL("DROP TABLE IF EXISTS " + table1 + ";");
        onCreate(db);
    }

}

Database_Acesso.java:

package workshopee.ct.ufrn.br.ssmonitor;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class Database_Acesso {
    private SQLiteDatabase db;

public Database_Acesso(Context context) {
    Database aux_db = new Database(context);
    db = aux_db.getWritableDatabase();
}
public void inserir_phone (Phone ph) {

    ContentValues valores = new ContentValues();
    valores.put("longitude",ph.getLongitude());
    valores.put("latitude", ph.getLatitude());
    valores.put("qtdtorres", ph.getTorres());
    valores.put("dbm", ph.getDbm());
    valores.put("mcc", ph.getMcc());
    valores.put("mnc", ph.getMnc());
    valores.put("phone_type", ph.getPhoneType());
    valores.put("operadora", ph.getOperadora());
    valores.put("cid",ph.getCid());
    valores.put("lac",ph.getLac());

    db.insert("phone", null, valores);
}


public List<Phone> buscar_phone () {
    List<Phone> lista = new ArrayList<Phone>();

    String[] colunas = new String[]{"_id", "longitude", "latitude", "qtdtorres", "dbm",
                        "mcc", "mnc", "phone_type", "operadora", "networkType", "cid", "lac"};

    Cursor cursor = db.query("phone", colunas, null, null, null, null,"_id ASC");
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
    }

   do {
       Phone p = new Phone();
       p.setId(cursor.getLong(0));
       p.setLongitude(cursor.getDouble(1));
       p.setLatitude(cursor.getDouble(2));
       p.setTorres(cursor.getInt(3));
       p.setDbm(cursor.getInt(4));
       p.setMcc(cursor.getInt(5));
       p.setMnc(cursor.getInt(6));
       p.setPhoneType(cursor.getString(7));
       p.setOperadora(cursor.getString(8));
       p.setNetWorkType(cursor.getString(9));
       p.setCid(cursor.getInt(10));
       p.setLac(cursor.getInt(11));
       lista.add(p);

   } while (!cursor.isLast());

    return lista;
  }

}

这是我的 MainActivity 中插入数据的部分:

    database_acesso.inserir_phone(cell);

其中 database_acesso 是 Database_acesso 的实例,单元格是 Phone 的实例。

下面是我尝试获取信息的方式: TextView list_text_view = (TextView) rootView.findViewById(R.id.list_text_view); 列表list = main.database_acesso.buscar_phone(); list_text_view.append(" - " + list.size());

我正在使用片段,所以 "main" 是 MainActivity 上的一个实例。 当我尝试执行它时,出现以下错误:

java.lang.OutOfMemoryError: [memory exhausted]
        at dalvik.system.NativeStart.main(Native Method)

这是完整的堆栈跟踪。

有什么解决办法吗?

谢谢。

您永远不会在 do { ... } while () 循环中调用 cursor.moveToNext(),因此您会不断创建新的 Phone 对象,直到 运行 内存不足。

一种可以说是更好的编写循环的方法是:

Cursor cursor = db.query("phone", colunas, null, null, null, null,"_id ASC");

while (cursor.moveToNext())
{
    // Do stuff
}

...since moveToNext() returns 一个布尔值,指示它是否已到达末尾。它还节省了调用 getCount().

的开销