2014-03-27 59 views
2

我正在處理Android應用程序,並試圖將SQLite數據庫數據作爲TextView(表中只有一條記錄)來獲取,並面臨此錯誤。CursorIndexOutOfBoundsException:請求索引1,大小爲1.錯誤 - Android

日誌和異常:

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

    } 

} 


} 

請幫我要避免這種錯誤。

+1

請問您可以在'TourInfo.java'中突出顯示**行號44 ** – MAC

+0

遊標爲空。 –

+0

索引1請求,大小爲1,這意味着該數組的大小爲1,因此使用0而不是使用1調用 –

回答

4

循環遍歷遊標中的光標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 
+0

感謝您的幫助 – user2881604

+0

謝謝!!!! :d – roseliux

-2

檢查表中是否有數據可用。

Cursor cursorJObInfo = jobInfoDatabaseHelper.getAllJobInfoRecords();是NULL,因爲表中可能沒有數據。

+0

表已創建,它有一個記錄 – user2881604

相關問題