java - CursorIndexOutOfBoundsException : Index 1 requested,,大小为 1。错误 - Android

标签 java android sqlite sqliteopenhelper

我正在开发 Android 应用程序,当我尝试将 SQLite 数据库数据获取为 TextView(表中只有一条记录)时遇到此错误。

日志和异常:

03-28 01:37:22.137: E/AndroidRuntime(9585): FATAL EXCEPTION: main
03-28 01:37:22.137: E/AndroidRuntime(9585): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tourinfo/com.example.tourinfo.TourInfo}: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.os.Looper.loop(Looper.java:123)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at java.lang.reflect.Method.invoke(Method.java:507)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at dalvik.system.NativeStart.main(Native Method)
03-28 01:37:22.137: E/AndroidRuntime(9585): Caused by: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at com.example.tourinfo.TourInfo.onCreate(TourInfo.java:44)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-28 01:37:22.137: E/AndroidRuntime(9585):     ... 11 more

这是java代码:

public class TourInfo extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tour_info);

    TourInfoDatabaseHelper jobInfoDatabaseHelper = new TourInfoDatabaseHelper(this);



    Cursor cursorJObInfo = jobInfoDatabaseHelper.getAllJobInfoRecords();
    if(cursorJObInfo.moveToFirst()){
        do{
            String startLocation = cursorJObInfo.getString(1);
            String endLocation = cursorJObInfo.getString(2);
            String type = cursorJObInfo.getString(3);
            Log.d("DB value", startLocation +" "+ endLocation +" "+ type);
        }while(cursorJObInfo.moveToNext());
    }


//  if(!cursorJObInfo.isClosed()){
//      cursorJObInfo.close();
 //   }
       TextView startLocationView = (TextView) findViewById(R.id.start_location_view);
        startLocationView.setText(cursorJObInfo.getString(1));
        TextView endLocationView = (TextView) findViewById(R.id.end_location_view);
        endLocationView.setText(cursorJObInfo.getString(2));
        TextView typeOfJourneyView = (TextView) findViewById(R.id.type_of_journey_view);
        typeOfJourneyView.setText(cursorJObInfo.getString(3));


}


public void onClickedLocations(View view){

    Intent intent = new Intent(this, LocationList.class);
    startActivity(intent);

}

}

TourInfoDatabaseHelper.java 类

  public class TourInfoDatabaseHelper {

 // private static final int DATABASE_VERSION= 2;
 // private static final String DATABASE_NAME = "tourinfo.db";
private static final String TABLE_NAME = "tour_info_details";

public static final String JOBINFO_COLUMN_JOB_ID = "job_id";
public static final String JOBINFO_COLUMN_START_LOCATION = "start_location";
public static final String JOBINFO_COLUMN_END_LOCATION = "end_location";
public static final String JOBINFO_COLUMN_TYPE = "type";
public static final String JOBINFO_COLUMN_TOUR_NUMBER = "tour_number";
public static final String JOBINFO_COLUMN_USER_ID ="user_id";


private SQLiteDatabase jobInfoDatabase;
private JobInfoOpenHelper jobInfoOpenHelper;

public TourInfoDatabaseHelper (Context context) {
    jobInfoOpenHelper = new JobInfoOpenHelper(context);
    jobInfoDatabase = jobInfoOpenHelper.getWritableDatabase();
} 

public void saveJobInfoRecords (String startLocation,String endLocation,String type,String tourNumber,int userId){

    ContentValues contentValues = new ContentValues();

//  contentValues.put(JOBINFO_COLUMN_JOB_ID, jobId);
    contentValues.put(JOBINFO_COLUMN_START_LOCATION, startLocation);
    contentValues.put(JOBINFO_COLUMN_END_LOCATION, endLocation);
    contentValues.put(JOBINFO_COLUMN_TYPE, type);
    contentValues.put(JOBINFO_COLUMN_TOUR_NUMBER, tourNumber);
    contentValues.put(JOBINFO_COLUMN_USER_ID, userId);

    jobInfoDatabase.insert(TABLE_NAME, null, contentValues);

}

public Cursor getAllJobInfoRecords(){
    return jobInfoDatabase.rawQuery(" SELECT * FROM " + TABLE_NAME, null);
}

private class JobInfoOpenHelper extends SQLiteOpenHelper {


    JobInfoOpenHelper(Context context) {
        super(context,MainDBAdapter.DATABASE_NAME,null,MainDBAdapter.DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase jobInfoDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase jobInfoDatabase, int oldVersion, int newVersion) {
    //  jobInfoDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    //  onCreate(jobInfoDatabase);

    }

}


}

请帮我避免这个错误。

最佳答案

您在 do-while 循环中遍历光标,但是当光标处于指向最后一个结果行之后。

有点不清楚您想做什么:您正在获取所有记录但只使用一个结果行。作为快速修复,捕获 do-while 而不是之后的值,例如

String startLocation = null;
String endLocation = null;
String type = null;

if(cursorJObInfo.moveToFirst()){
    do{
        startLocation = cursorJObInfo.getString(1);
        endLocation = cursorJObInfo.getString(2);
        type = cursorJObInfo.getString(3);
        Log.d("DB value", startLocation +" "+ endLocation +" "+ type);
    }while(cursorJObInfo.moveToNext());
}

// ...
startLocationView.setText(startLocation); // instead of calling getString() on the cursor here
// the same for the other textviews

关于java - CursorIndexOutOfBoundsException : Index 1 requested,,大小为 1。错误 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22682078/

相关文章:

c++ - c++ 中的 sqlite - 来自不同应用程序的并行插入,会发生什么?

java - 类加载器、类差异

java - 当广播接收器收到消息时更新 recyclerview 上的切换按钮

android - Firebase 远程配置 - 初始提取返回本地默认值

php - PDOstatement有问题,其中不包含应包含的内容

java - 我收到错误隐式 super 构造函数 SQLiteOpenHelper()

java - 重置随机生成器 Android

java - rJava加载错误

java - 在Java中获取ASCII-6编码

android - 无法创建任务或键入 checkenv 原因名称未定义