我在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);
}
}
}
请帮助我避免这个错误。
3条答案
按热度按时间ca1c2owp1#
在
do
-while
循环中循环遍历游标,但当游标指向最后一个结果行之后时,访问游标数据时在getString()
之后。我不太清楚你想做什么:您正在获取所有记录,但只使用了一个结果行。作为快速修复,捕获
do
-while
内部的值,而不是之后的值,例如klh5stk12#
如果您将来遇到类似的错误,这是问题的原因:
在检查游标是否为空之前重置索引,因为
do..while
在检查是否满足条件之前至少执行一次。要避免此错误,请尝试while...do
。您甚至可以忽略do
部分:首先使用
Cursor.geCount
检查光标是否为空然后移动到第一个记录。注意:确保你不在循环中,否则你将进入无限循环。
然后使用
while..do
遍历游标7d7tgy0s3#
检查表中是否有可用数据。
这个
Cursor cursorJObInfo = jobInfoDatabaseHelper.getAllJobInfoRecords();
是NULL,因为表中可能没有数据。