当数据库中不存在搜索值时,Android应用程序崩溃

时间:2015-04-27 12:06:30

标签: android sqlite

当我搜索数据库中不存在的任何值时,应用程序会立即崩溃。

public String ifExistIn(String stationName) {
    String query = "SELECT stationName FROM review WHERE stationId" +
            "='" + stationName + "' OR " + " stationName " + "='" + stationName + "' LIMIT 1";
    try (SQLiteDatabase database = this.getWritableDatabase();
         Cursor c = database.rawQuery(query, null)) {
        c.moveToFirst();
        return c.getString(c.getColumnIndex("stationName"));
    }
}

Activity将可用记录添加到ListView

public View.OnClickListener searchStation = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String searchString = searchText.getText().toString();
        //String useThis = dbHelper.ifExistIn(searchString);        
        if (list.contains(dbms.ifExistIn(searchString))) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SearchAndReview.this);
                builder.setTitle("");
                builder.setMessage("RECORD ALREADY IN THE LIST ");
                builder.setNeutralButton("OK", null);
                builder.create().show();
            else {
                list.add(dbms.ifExistIn(searchString));
                listView.setAdapter(arrayAdapter);
            }
        } 
    }
};

Android工作室日志详细信息致命异常

04-27 13:13:27.386  19845-19845/com.example.fahimghl.reviewapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.fahimghl.reviewapplication, PID: 19845
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
        at com.example.fahimghl.reviewapplication.DatabaseHelper.ifExistIn(DatabaseHelper.java:130)
        at com.example.fahimghl.reviewapplication.SearchAndReview$10.onClick(SearchAndReview.java:172)
        at android.view.View.performClick(View.java:4761)
        at android.view.View$PerformClick.run(View.java:19767)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

2 个答案:

答案 0 :(得分:1)

  

当我搜索数据库应用程序中不存在的任何值时崩溃   瞬间。

然后你可能正在查询一个空的Cursor。

try (SQLiteDatabase database = this.getWritableDatabase();
        Cursor c = database.rawQuery(query, null)) {
        if (c != null && c.moveToFirst()) {
           return c.getString(c.getColumnIndex("stationName"));
        }
}
return null;

请注意,try-with-resources至少需要api级别19

答案 1 :(得分:0)

    public String ifExistIn(String stationName) {

String query = "SELECT stationName FROM review WHERE stationId" +
        "='" + stationName + "' OR " + " stationName " + "='" + stationName + "' LIMIT 1";
try (SQLiteDatabase database = this.getWritableDatabase();
     Cursor c = database.rawQuery(query, null))


 if (cursor.moveToFirst()) {
        do {
    String stationName = c.getString(c.getColumnIndex("stationName"));
  } while (cursor.moveToNext());
    }
    database.close();
    c.close();

return stationName;
}

请始终关闭数据库和光标