Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当搜索到的值不';不存在于数据库中_Android_Sqlite - Fatal编程技术网

Android 当搜索到的值不';不存在于数据库中

Android 当搜索到的值不';不存在于数据库中,android,sqlite,Android,Sqlite,当我搜索数据库中不存在的任何值时,应用程序立即崩溃 public String ifExistIn(String stationName) { String query = "SELECT stationName FROM review WHERE stationId" + "='" + stationName + "' OR " + " stationName " + "='" + stationName + "' LIMIT 1"; try (SQLi

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

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"));
    }
}
活动将可用记录添加到
列表视图中

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 studio日志详细信息致命异常

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)
当我搜索数据库中不存在的任何值时,应用程序崩溃 马上

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"));
    }
}
那么您可能正在查询一个空游标

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

    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;
}

请始终关闭数据库,并用光标

post日志显示该列表已初始化?它已初始化。当数据库中不存在值时,它的给定问题。当我搜索右值时,它不会崩溃、返回和填充列表视图。给我一分钟,我将在那里发布日志,你可以查看。请检查更新后的日志,并在这里询问日志