我試圖從我的數據庫中獲取一些信息。我是Android的初學者。 我有一個名爲「Database」的數據庫創建類和一個名爲「Database_Acesso」的數據庫訪問類。他們看起來像這樣:嘗試從數據庫獲取數據時耗盡內存(android)
Database.java: package 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的實例,電池是手機的一個實例。
這裏是我如何獲取信息: TextView list_text_view =(TextView)rootView.findViewById(R.id.list_text_view); List 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)
它是完整的堆棧跟蹤。
解決這個問題的任何想法?
Thx。
你是怎麼運行的?在模擬器上?也許沒有足夠的內存分配給模擬器? – javajavajava