gpt4 book ai didi

android - 逻辑上正确但 'if statement' 的方法使我的应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 15:26:00 26 4
gpt4 key购买 nike

我的 MapActivity 中有一个 AsyncTask 类。在 doInBacground() 方法中,我必须使用代码块。

看起来像这样:

        @Override
protected List<GeoPoint> doInBackground(String... arg0) {


// check if the query (word and the distance) existing queries and return its id
int checkQuery = db.getQueryMatch(arg0[3].toString() , arg0[2].toString());
Log.v(" CheckQuery int ", " equal? " + checkQuery );

if (checkQuery < 1)
{
Block 1
}
else
{
Block 2
}

getQueryMatch 方法将检查两个文本字段是否存在,然后返回记录 ID。

int getQueryMatch(String word, String meter) {
SQLiteDatabase db = this.getReadableDatabase();
int getidd = 0;
Cursor cursor = db.query(TABLE_HISTORY, new String[] { KEY_ID, QUERYWORD, DISTANCEQ, KEY_TIMESTAMP }, QUERYWORD + "=? AND " + DISTANCEQ + "=?", new String[] { word, meter }, null, null, null, null);
//String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + QUERYWORD + "= '" + word + "' AND " + DISTANCEQ + "= '"+ meter +"'" ;
if (cursor != null)
cursor.moveToFirst();
getidd = Integer.parseInt(cursor.getString(0));

db.close(); // Closing database connection

// return History
return getidd;
}

当我在 AsyncTask 类中不使用 if 语句 进行测试时,它工作正常。如果返回值为 0,它会使应用程序崩溃。所以我想做的是,如果没有从数据库中找到记录(由 getQueryMatch 检查),则运行 block 1,否则运行 block 2。

错误如下:

10-19 02:47:50.464: E/AndroidRuntime(11967): FATAL EXCEPTION: AsyncTask #2
10-19 02:47:50.464: E/AndroidRuntime(11967): java.lang.RuntimeException: An error occured while executing doInBackground()
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.lang.Thread.run(Thread.java:856)
10-19 02:47:50.464: E/AndroidRuntime(11967): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
10-19 02:47:50.464: E/AndroidRuntime(11967): at com.example.phooogle.DatabaseHandler.getQueryMatch(DatabaseHandler.java:148)
10-19 02:47:50.464: E/AndroidRuntime(11967): at com.example.phooogle.GoogleMapsAppActivity$InitTask.doInBackground(GoogleMapsAppActivity.java:142)
10-19 02:47:50.464: E/AndroidRuntime(11967): at com.example.phooogle.GoogleMapsAppActivity$InitTask.doInBackground(GoogleMapsAppActivity.java:1)
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

对我来说,这在逻辑上是正确的,但行不通!

最佳答案

您需要检查 movetofirst 返回的值。如果游标为空则返回 false

   boolean notEmpty = cursor.moveToFirst();
if(notEmpty)
getidd = Integer.parseInt(cursor.getString(0));

编辑:

getidd = -1;
if (cursor != null){
boolean notEmpty = cursor.moveToFirst();
if(notEmpty)
getidd = Integer.parseInt(cursor.getString(0));
}
db.close(); // Closing database connection
// return History
return getidd;

关于android - 逻辑上正确但 'if statement' 的方法使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12958693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com