1

我的 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 检查),则运行块 1,否则运行块 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)

对我来说,这在逻辑上是正确的,但不起作用!

4

2 回答 2

5

您需要检查 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;
于 2012-10-18T15:59:07.683 回答
0

显然,cursor.getString(0)失败是因为没有任何数据。

您还必须检查 的返回值cursor.moveToFirst()

于 2012-10-18T16:02:54.803 回答