我的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,它会使应用程序崩溃。 所以我想要做的是在没有从DB找到记录的情况下运行块1(由getQueryMatch检查)否则运行块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)
对我而言,这在逻辑上是正确的但不起作用!
答案 0 :(得分: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;
答案 1 :(得分:0)
显然,cursor.getString(0)
失败,因为没有任何数据。
您还必须检查cursor.moveToFirst()
的返回值。