我正在使用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);
}
}
}
请帮我避免此错误。
答案 0 :(得分:7)
在do
- while
循环中循环浏览光标,但在光标指向最后一个结果行之后,访问光标数据后跟getString()
。
有点不清楚你想做什么:你正在获取所有记录,但只使用一个结果行。作为快速修复,请捕获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
答案 1 :(得分:-2)
检查表中是否有可用的数据。
此Cursor cursorJObInfo = jobInfoDatabaseHelper.getAllJobInfoRecords();
为NULL,因为表中可能没有数据。