java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
我正在尝试比较用户提供的经纬度值与数据库中的经纬度值。如果它们在彼此半径 15 公里内,则应更改文本视图。
但我面临以下错误,
我的数据库包含值 source lat = 19.218418 source long = 73.08661
经纬度 = 18.9766017 经度 = 72.8326658。用户提供的值是
source long = 19.2213935 source long = 73.081532 des lat = 18.9632933 des long = 72.8324848.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hitesh.nearby, PID: 2156
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5347)
at android.view.View.performClick(View.java:6267)
at android.view.View$PerformClick.run(View.java:24763)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5342)
at android.view.View.performClick(View.java:6267)
at android.view.View$PerformClick.run(View.java:24763)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
Result.java
public class result extends Activity {
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
myDb = new DatabaseHelper(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_result);
String url = getIntent().getExtras().getString("username");
String url1 = getIntent().getExtras().getString("username1");
TextView tv= (TextView)findViewById(R.id.textView2);
TextView tv1= (TextView)findViewById(R.id.textView5);
String[] separated = url.split("_");
String srclat = separated[0];
Double dsrclat= Double.parseDouble(srclat);
String srclng = separated[1];
Double dsrclng= Double.parseDouble(srclng);
String[] separated1 = url.split("_");
String deslat = separated1[0];
Double ddeslat= Double.parseDouble(deslat);
String deslng = separated1[1];
Double ddeslng= Double.parseDouble(deslng);
tv.setText(url);
tv1.setText(url1);
}
public void Database(View view){
Intent intent = new Intent(result.this, feeder.class);
startActivity(intent);
}
public void NearestValue(View view){
TextView tv= (TextView)findViewById(R.id.textView2);
String s1 = tv.getText().toString();
String[] separated = s1.split("_");
String deslat = separated[0];
Double Lat= Double.parseDouble(deslat);
String deslng = separated[1];
Double Lng= Double.parseDouble(deslng);
ArrayList<String> ssList = new ArrayList<>();
ssList = myDb.countDatabase(Lat,Lng);
int ssize = ssList.size();
TextView tvvv = (TextView) findViewById(R.id.textView8);
for(int i=0;i<ssize;i++) {
tvvv.setText(ssList.get(i));
}
TextView tv1= (TextView)findViewById(R.id.textView5);
String s11 = tv1.getText().toString();
String[] separated1 = s11.split("_");
String deslat1 = separated1[0];
Double Lat1= Double.parseDouble(deslat1);
String deslng1 = separated1[1];
Double Lng1= Double.parseDouble(deslng1);
ArrayList<String> ddList = new ArrayList<>();
ddList = myDb.countDatabase1(Lat1,Lng1);
int dsize = ddList.size();
TextView tvvvv = (TextView) findViewById(R.id.textView9);
for(int i=0;i<dsize;i++) {
tvvv.setText(ddList.get(i));
}
for(int i=0;i<ssize;i++){
String source = ssList.get(i);
for(int j=0;j<dsize;j++) {
String destination = ddList.get(j);
String src = myDb.findBus(source, destination);
TextView tvv = (TextView) findViewById(R.id.textView7);
tvv.setText(src);
}
}
}
}
数据库助手
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Nearby.db";
public static final String TABLE_NAME = "halt_details";
public static final String COL_2 = "snme";
public static final String COL_3 = "slati";
public static final String COL_4 = "slong";
public static final String COL_5 = "dnme";
public static final String COL_6 = "dlati";
public static final String COL_7 = "dlong";
public static final String COL_8 = "buses";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, snme VARCHAR, slati DOUBLE, slong DOUBLE, dnme VARCHAR, dlati DOUBLE, dlong DOUBLE, buses VARCHAR)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertData(String sname, Double slat, Double slng, String dname, Double dlat, Double dlng, String bus) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("snme",sname);
contentValues.put("slati",slat);
contentValues.put("slong",slng);
contentValues.put("dnme",dname);
contentValues.put("dlati",dlat);
contentValues.put("dlong",dlng);
contentValues.put("buses",bus);
db.insert(TABLE_NAME,null ,contentValues);
}
public void clearDatabase() {
SQLiteDatabase db = this.getWritableDatabase();
String clearDBQuery = "DELETE FROM "+TABLE_NAME;
db.execSQL(clearDBQuery);
}
public ArrayList<String> countDatabase(Double LAT, Double LNG) {
String sourcename=null;
double earthRadius = 6371.75;
ArrayList<String> sList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_3 + " BETWEEN (" +LAT+"- 0.1) AND ("+LAT+" + 0.1) AND " + DatabaseHelper.COL_4 + " BETWEEN (" +LNG+"- 0.1) AND ("+LNG+" + 0.1)", null);
mCount.moveToNext();
while (mCount.moveToFirst()) {
double Latitude = mCount.getDouble(2);
double Longitude = mCount.getDouble(3);
double dLat = Math.toRadians(Latitude-LAT);
double dLng = Math.toRadians(Longitude-LNG);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(LAT)) * Math.cos(Math.toRadians(Latitude));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
if (dist<15){
sourcename=mCount.getString(1);
sList.add(sourcename);
}
}
mCount.close();
return sList;
}
public ArrayList<String> countDatabase1(Double LAT, Double LNG) {
String destinationname=null;
ArrayList<String> dList = new ArrayList<>();
double earthRadius = 6371.75;
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_3 + " BETWEEN (" +LAT+"- 0.1) AND ("+LAT+" + 0.1) AND " + DatabaseHelper.COL_4 + " BETWEEN (" +LNG+"- 0.1) AND ("+LNG+" + 0.1)", null);
mCount.moveToFirst();
while (mCount.moveToNext() ) {
double Latitude = mCount.getDouble(2);
double Longitude = mCount.getDouble(3);
double dLat = Math.toRadians(Latitude-LAT);
double dLng = Math.toRadians(Longitude-LNG);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(LAT)) * Math.cos(Math.toRadians(Latitude));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
if (dist<15){
destinationname = mCount.getString(4);
dList.add(destinationname);
}
}
mCount.close();
return dList;
}
public String findBus(String sourcename, String desname){
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("select count(*) from search_table where name='" + sourcename + "' AND dname='"+desname+"'", null);
mCount.moveToFirst();
String src= mCount.getString(1);
mCount.close();
return src;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
}
我建议您在 findBus
方法和调用它的循环中遇到一些问题。
1。不必要的循环
围绕 findBus
方法调用的 3 个循环似乎没有用,因为它们重复设置 TextView
但只有最后一个可能可见并因此可用。您可以使用 size
方法直接转到 ArrayList
的最后一个元素。
我建议如下:-
tvvv.setText(ddList.get(ddList.size()-1));
TextView tvv = (TextView) findViewById(R.id.textView7);
tvv.setText(myDB.findBus(ssList.get(ssList.size()-1),ddList.get(ddList.size()-1)));
应该替换:-
for(int i=0;i<dsize;i++) {
tvvv.setText(ddList.get(i));
}
for(int i=0;i<ssize;i++){
String source = ssList.get(i);
for(int j=0;j<dsize;j++) {
String destination = ddList.get(j);
String src = myDb.findBus(source, destination);
TextView tvv = (TextView) findViewById(R.id.textView7);
tvv.setText(src);
}
}
}
2。偏移量无效
此外,在 findBus 方法中,您试图访问从查询返回的第二列,而查询 returns 只有 1 列。也就是说,您编码了偏移量 1 而只有偏移量 0 (列 count(*))是唯一的可用列。
我建议将 findBus
方法更改为:-
public String findBus(String sourcename, String desname){
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("select count(*) from search_table where name='" + sourcename + "' AND dname='"+desname+"'", null);
mCount.moveToFirst();
String src= mCount.getString(0); //<<<< Changed offset to 0 from 1
mCount.close();
return src;
}
这可能会减少内存需求,如果足够的话我不知道。但是,您可能还希望检查右侧显示的 Related 链接关于 OOM(内存不足错误,可能是 (Andorid)).
查看您的代码似乎是循环问题。
您在使用游标循环时没有移动到下一个。您首先使用了 moveToNext 并且在 while 条件下使用了 moveToFirst
更新您的代码如下
public ArrayList<String> countDatabase(Double LAT, Double LNG) {
String sourcename=null;
double earthRadius = 6371.75;
ArrayList<String> sList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_3 + " BETWEEN (" +LAT+"- 0.1) AND ("+LAT+" + 0.1) AND " + DatabaseHelper.COL_4 + " BETWEEN (" +LNG+"- 0.1) AND ("+LNG+" + 0.1)", null);
mCount.moveToFirst();
while (mCount.moveToNext()) {
double Latitude = mCount.getDouble(2);
double Longitude = mCount.getDouble(3);
double dLat = Math.toRadians(Latitude-LAT);
double dLng = Math.toRadians(Longitude-LNG);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(LAT)) * Math.cos(Math.toRadians(Latitude));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
if (dist<15){
sourcename=mCount.getString(1);
sList.add(sourcename);
}
}
mCount.close();
return sList;
}
此外,您正在创建数据库对象,但在使用后没有关闭它。
您需要确保关闭游标以及数据库对象。
我还看到您正在从您的方法中 returning 光标。如果你从游标中获取你想要的数据,关闭游标并return你想要的数据,那将是很好的。
public String findBus(String sourcename, String desname){
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("select count(*) from search_table where name='" + sourcename + "' AND dname='"+desname+"'", null);
mCount.moveToFirst();
String src= mCount.getString(1);
mCount.close();
db.close();
return src;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
//Should return the data and close cursor
return res;
}
希望这能解决您的问题。
我正在尝试比较用户提供的经纬度值与数据库中的经纬度值。如果它们在彼此半径 15 公里内,则应更改文本视图。 但我面临以下错误,
我的数据库包含值 source lat = 19.218418 source long = 73.08661 经纬度 = 18.9766017 经度 = 72.8326658。用户提供的值是 source long = 19.2213935 source long = 73.081532 des lat = 18.9632933 des long = 72.8324848.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hitesh.nearby, PID: 2156
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5347)
at android.view.View.performClick(View.java:6267)
at android.view.View$PerformClick.run(View.java:24763)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5342)
at android.view.View.performClick(View.java:6267)
at android.view.View$PerformClick.run(View.java:24763)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
Result.java
public class result extends Activity {
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
myDb = new DatabaseHelper(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_result);
String url = getIntent().getExtras().getString("username");
String url1 = getIntent().getExtras().getString("username1");
TextView tv= (TextView)findViewById(R.id.textView2);
TextView tv1= (TextView)findViewById(R.id.textView5);
String[] separated = url.split("_");
String srclat = separated[0];
Double dsrclat= Double.parseDouble(srclat);
String srclng = separated[1];
Double dsrclng= Double.parseDouble(srclng);
String[] separated1 = url.split("_");
String deslat = separated1[0];
Double ddeslat= Double.parseDouble(deslat);
String deslng = separated1[1];
Double ddeslng= Double.parseDouble(deslng);
tv.setText(url);
tv1.setText(url1);
}
public void Database(View view){
Intent intent = new Intent(result.this, feeder.class);
startActivity(intent);
}
public void NearestValue(View view){
TextView tv= (TextView)findViewById(R.id.textView2);
String s1 = tv.getText().toString();
String[] separated = s1.split("_");
String deslat = separated[0];
Double Lat= Double.parseDouble(deslat);
String deslng = separated[1];
Double Lng= Double.parseDouble(deslng);
ArrayList<String> ssList = new ArrayList<>();
ssList = myDb.countDatabase(Lat,Lng);
int ssize = ssList.size();
TextView tvvv = (TextView) findViewById(R.id.textView8);
for(int i=0;i<ssize;i++) {
tvvv.setText(ssList.get(i));
}
TextView tv1= (TextView)findViewById(R.id.textView5);
String s11 = tv1.getText().toString();
String[] separated1 = s11.split("_");
String deslat1 = separated1[0];
Double Lat1= Double.parseDouble(deslat1);
String deslng1 = separated1[1];
Double Lng1= Double.parseDouble(deslng1);
ArrayList<String> ddList = new ArrayList<>();
ddList = myDb.countDatabase1(Lat1,Lng1);
int dsize = ddList.size();
TextView tvvvv = (TextView) findViewById(R.id.textView9);
for(int i=0;i<dsize;i++) {
tvvv.setText(ddList.get(i));
}
for(int i=0;i<ssize;i++){
String source = ssList.get(i);
for(int j=0;j<dsize;j++) {
String destination = ddList.get(j);
String src = myDb.findBus(source, destination);
TextView tvv = (TextView) findViewById(R.id.textView7);
tvv.setText(src);
}
}
}
}
数据库助手
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Nearby.db";
public static final String TABLE_NAME = "halt_details";
public static final String COL_2 = "snme";
public static final String COL_3 = "slati";
public static final String COL_4 = "slong";
public static final String COL_5 = "dnme";
public static final String COL_6 = "dlati";
public static final String COL_7 = "dlong";
public static final String COL_8 = "buses";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, snme VARCHAR, slati DOUBLE, slong DOUBLE, dnme VARCHAR, dlati DOUBLE, dlong DOUBLE, buses VARCHAR)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertData(String sname, Double slat, Double slng, String dname, Double dlat, Double dlng, String bus) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("snme",sname);
contentValues.put("slati",slat);
contentValues.put("slong",slng);
contentValues.put("dnme",dname);
contentValues.put("dlati",dlat);
contentValues.put("dlong",dlng);
contentValues.put("buses",bus);
db.insert(TABLE_NAME,null ,contentValues);
}
public void clearDatabase() {
SQLiteDatabase db = this.getWritableDatabase();
String clearDBQuery = "DELETE FROM "+TABLE_NAME;
db.execSQL(clearDBQuery);
}
public ArrayList<String> countDatabase(Double LAT, Double LNG) {
String sourcename=null;
double earthRadius = 6371.75;
ArrayList<String> sList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_3 + " BETWEEN (" +LAT+"- 0.1) AND ("+LAT+" + 0.1) AND " + DatabaseHelper.COL_4 + " BETWEEN (" +LNG+"- 0.1) AND ("+LNG+" + 0.1)", null);
mCount.moveToNext();
while (mCount.moveToFirst()) {
double Latitude = mCount.getDouble(2);
double Longitude = mCount.getDouble(3);
double dLat = Math.toRadians(Latitude-LAT);
double dLng = Math.toRadians(Longitude-LNG);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(LAT)) * Math.cos(Math.toRadians(Latitude));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
if (dist<15){
sourcename=mCount.getString(1);
sList.add(sourcename);
}
}
mCount.close();
return sList;
}
public ArrayList<String> countDatabase1(Double LAT, Double LNG) {
String destinationname=null;
ArrayList<String> dList = new ArrayList<>();
double earthRadius = 6371.75;
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_3 + " BETWEEN (" +LAT+"- 0.1) AND ("+LAT+" + 0.1) AND " + DatabaseHelper.COL_4 + " BETWEEN (" +LNG+"- 0.1) AND ("+LNG+" + 0.1)", null);
mCount.moveToFirst();
while (mCount.moveToNext() ) {
double Latitude = mCount.getDouble(2);
double Longitude = mCount.getDouble(3);
double dLat = Math.toRadians(Latitude-LAT);
double dLng = Math.toRadians(Longitude-LNG);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(LAT)) * Math.cos(Math.toRadians(Latitude));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
if (dist<15){
destinationname = mCount.getString(4);
dList.add(destinationname);
}
}
mCount.close();
return dList;
}
public String findBus(String sourcename, String desname){
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("select count(*) from search_table where name='" + sourcename + "' AND dname='"+desname+"'", null);
mCount.moveToFirst();
String src= mCount.getString(1);
mCount.close();
return src;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
}
我建议您在 findBus
方法和调用它的循环中遇到一些问题。
1。不必要的循环
围绕 findBus
方法调用的 3 个循环似乎没有用,因为它们重复设置 TextView
但只有最后一个可能可见并因此可用。您可以使用 size
方法直接转到 ArrayList
的最后一个元素。
我建议如下:-
tvvv.setText(ddList.get(ddList.size()-1));
TextView tvv = (TextView) findViewById(R.id.textView7);
tvv.setText(myDB.findBus(ssList.get(ssList.size()-1),ddList.get(ddList.size()-1)));
应该替换:-
for(int i=0;i<dsize;i++) {
tvvv.setText(ddList.get(i));
}
for(int i=0;i<ssize;i++){
String source = ssList.get(i);
for(int j=0;j<dsize;j++) {
String destination = ddList.get(j);
String src = myDb.findBus(source, destination);
TextView tvv = (TextView) findViewById(R.id.textView7);
tvv.setText(src);
}
}
}
2。偏移量无效
此外,在 findBus 方法中,您试图访问从查询返回的第二列,而查询 returns 只有 1 列。也就是说,您编码了偏移量 1 而只有偏移量 0 (列 count(*))是唯一的可用列。
我建议将 findBus
方法更改为:-
public String findBus(String sourcename, String desname){
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("select count(*) from search_table where name='" + sourcename + "' AND dname='"+desname+"'", null);
mCount.moveToFirst();
String src= mCount.getString(0); //<<<< Changed offset to 0 from 1
mCount.close();
return src;
}
这可能会减少内存需求,如果足够的话我不知道。但是,您可能还希望检查右侧显示的 Related 链接关于 OOM(内存不足错误,可能是 (Andorid)).
查看您的代码似乎是循环问题。
您在使用游标循环时没有移动到下一个。您首先使用了 moveToNext 并且在 while 条件下使用了 moveToFirst
更新您的代码如下
public ArrayList<String> countDatabase(Double LAT, Double LNG) {
String sourcename=null;
double earthRadius = 6371.75;
ArrayList<String> sList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_3 + " BETWEEN (" +LAT+"- 0.1) AND ("+LAT+" + 0.1) AND " + DatabaseHelper.COL_4 + " BETWEEN (" +LNG+"- 0.1) AND ("+LNG+" + 0.1)", null);
mCount.moveToFirst();
while (mCount.moveToNext()) {
double Latitude = mCount.getDouble(2);
double Longitude = mCount.getDouble(3);
double dLat = Math.toRadians(Latitude-LAT);
double dLng = Math.toRadians(Longitude-LNG);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(LAT)) * Math.cos(Math.toRadians(Latitude));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
if (dist<15){
sourcename=mCount.getString(1);
sList.add(sourcename);
}
}
mCount.close();
return sList;
}
此外,您正在创建数据库对象,但在使用后没有关闭它。
您需要确保关闭游标以及数据库对象。 我还看到您正在从您的方法中 returning 光标。如果你从游标中获取你想要的数据,关闭游标并return你想要的数据,那将是很好的。
public String findBus(String sourcename, String desname){
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("select count(*) from search_table where name='" + sourcename + "' AND dname='"+desname+"'", null);
mCount.moveToFirst();
String src= mCount.getString(1);
mCount.close();
db.close();
return src;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
//Should return the data and close cursor
return res;
}
希望这能解决您的问题。