- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试将用户提供的经纬度值与数据库中的经纬度值进行比较。如果它们在彼此半径 15 公里内,则应更改 TextView 。但我面临以下错误,
我的数据库包含值 source lat = 19.218418 source long = 73.08661经纬度 = 18.9766017 经度 = 72.8326658。用户提供的值是source lat = 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
结果.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
方法和调用它的循环中遇到一些问题。
围绕调用 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);
}
}
}
此外,在 findBus 方法中,当查询仅返回 1 列时,您正试图访问从查询返回的第 2 列。即您编码了偏移量 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;
}
这可能会减少内存需求,如果足够的话我不知道。但是,您可能还希望检查右侧显示的关于 OOM(内存不足错误,可能是 (Andorid))的相关链接。
关于java.lang.OutOfMemoryError : OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49461331/
在编写代码时,当我编写 struct *Stack stack 时,代码无法工作,而当我像 struct Stack* stack 那样编写代码时,代码可以工作有人告诉我这两个代码之间的区别吗? 最佳
我的教授要我绘制堆栈吗?他想让我付诸行动吗?我觉得自己很愚蠢,但这不像任何人告诉我的那样!感谢您的帮助。 哇,你们真快。已经谢谢你了。完整的问题是:考虑两个堆栈,每个堆栈的大小为 n(即,每个堆栈最多
是new Stack[N]相当于new Stack[N]对于通用数据类型 Stack ? 编辑:虽然我知道最好避免混合泛型类型和数组,并且存在更强大的解决方案,但我的查询仍然存在:被广泛认可的教科书,
我正在尝试使用堆栈的 printf() 打印 stack.top() 的返回值,但它给出的格式不匹配。代码如下: int main(){ stack cards; char *ch1
我正在尝试做一个 dapp 项目。 我有一个堆栈太深的错误,但我不知道如何解决这个问题。 CompilerError: Stack too deep, try removing local varia
在哪里stack haddock (或 stack build --haddock )放置它生成的文档? 最佳答案 这取决于为“属于”生成黑线鳕的包的位置。 “本地”包的 Haddocks 是堆栈项目
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
基本上,我有 2 个文件(.adb 和 .ads)。我对 Ada 以及如何编译 2 个文件完全陌生。该程序是一个基本的堆栈实现。编译 .adb 文件时出现此编译错误。 $ gcc -c test_ad
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 5年前关闭。 Improve this questi
您好,我正在尝试在微服务架构中使用 ELK 堆栈,其中有许多服务分布在许多服务器上。 现在我已经配置了 Kibana 和 ElasticSearch。现在我的疑问是我必须在哪里安装 Logstash。
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我正在学习 Haskell,但遇到了我没想到的异常“堆栈溢出”。 代码相当简单: type Reals = Double prod :: Reals -> Reals -> Reals prod a
在我的 PC(最新的 JDK 和编译器版本)上,我的程序运行得非常顺利。但在我教授的计算机上,她说她遇到了两个错误 1) Stack myStack= new Stack() 类型的非法开始 2) 非
当我尝试在调用跟踪后执行 ./stack.sh 时,我收到以下错误: `[Call Trace] ./stack.sh:217:source /home/work/devstack/stackrc
1 上下文 我参与了一个涉及大量 C 位和 FFI 的 Haskell 项目。所以我发现自己经常运行和重新运行命令,比如 $ stack build $ stack build --force-dir
关于 SO 上的堆栈的问题,终于!我的一生都让我走到了这一步。 所以我需要将我制作的一些相当大的自定义数据结构合并到一个堆栈中。我决定编写一个仅包含一个整数值的最小堆栈结构。这里是 - MODULE
我只是想知道为什么在括号之间嵌入表达式时会得到两个不同的成员列表,例如gl-stack。看起来,如果没有括号,表达式就会被完全求值,并且结果会立即传递到下一个管道组件。但通过括号,集合中的单个对象将被
是否可以保存Stack>在onSaveInstanceState . 是否可以通过另一种方式保存某些特定数据来管理 Activity 状态? 最佳答案 您无法将 View 保存到 bundle 中。无
这个问题已经有答案了: Why don't Java Generics support primitive types? (5 个回答) 已关闭 9 年前。 为什么我不能使用Stack ? 除了拳击之
基于数组的实现级别 #include "stack.h" void creat_stack(Stack *s) { s->Top = 0; } int isFull(Stack s) {
我是一名优秀的程序员,十分优秀!