当我尝试在SQLite表中添加新行时收到错误,并且在我修复了另一个问题后发生了这个问题。
Logcat表示问题是由某种方法引起的,但我真的不知道我的错误在哪里。
LogCat:
02-28 12:28:45.019: E/AndroidRuntime(432): FATAL EXCEPTION: main
02-28 12:28:45.019: E/AndroidRuntime(432): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-28 12:28:45.019: E/AndroidRuntime(432): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
02-28 12:28:45.019: E/AndroidRuntime(432): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
02-28 12:28:45.019: E/AndroidRuntime(432): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
02-28 12:28:45.019: E/AndroidRuntime(432): at com.androidbook.MP.MP_DB.getMP_Time(MP_DB.java:62)
02-28 12:28:45.019: E/AndroidRuntime(432): at com.androidbook.MP.MPData.retrieveRecords(MPData.java:77)
02-28 12:28:45.019: E/AndroidRuntime(432): at com.androidbook.MP.MPData$MyLocationListener.onLocationChanged(MPData.java:181)
02-28 12:28:45.019: E/AndroidRuntime(432): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
02-28 12:28:45.019: E/AndroidRuntime(432): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
02-28 12:28:45.019: E/AndroidRuntime(432): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
02-28 12:28:45.019: E/AndroidRuntime(432): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 12:28:45.019: E/AndroidRuntime(432): at android.os.Looper.loop(Looper.java:130)
02-28 12:28:45.019: E/AndroidRuntime(432): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-28 12:28:45.019: E/AndroidRuntime(432): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 12:28:45.019: E/AndroidRuntime(432): at java.lang.reflect.Method.invoke(Method.java:507)
02-28 12:28:45.019: E/AndroidRuntime(432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-28 12:28:45.019: E/AndroidRuntime(432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-28 12:28:45.019: E/AndroidRuntime(432): at dalvik.system.NativeStart.main(Native Method)
Meothos Logcat表明它会导致错误:
public void retrieveRecords() {
int c = mpoh.getCurrentRowNumber();
String t = mpoh.getMP_Time(c);
String d = mpoh.getMP_Date(c);
double pitch = mpRM.getPich();
double yaw = mpRM.getYaw();
tv_Time = (TextView) findViewById(R.id.tv_Time_Value);
tv_Date = (TextView) findViewById(R.id.tv_Date_Value);
tv_pitch = (TextView) findViewById(R.id.tv_Pitch_Value);
tv_yaw = (TextView) findViewById(R.id.tv_Yaw_Value);
tv_Time.setText(t);
tv_Date.setText(d);
tv_pitch.setText(Double.toString(pitch));
tv_yaw.setText(Double.toString(yaw));
}
MP_DB:
public class MP_DB extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "MP.db";
private static final String MP_TABLE_NAME = "MPData";
MP_DB (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}// end of
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(" CREATE TABLE " + MP_TABLE_NAME + " ( " +
BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT, " +
" lat REAL, " +
" lng REAL, " +
" date TEXT, " +
" time TEXT " +
");" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public String getMP_Name(long id) {
SQLiteDatabase db = this.getReadableDatabase();
SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT name FROM MPData WHERE "+
BaseColumns._ID+" = "+
Long.toString(id) +" AND name IS NOT NULL ", null);
c.moveToFirst();
String r = c.getString(0);
c.close();
db.close();
return r;
}
public int countRows(SQLiteCursor c) {
return c.getCount();
}
public String getMP_Time(long id) {
SQLiteDatabase db = this.getReadableDatabase();
SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT time FROM MPData WHERE "+
BaseColumns._ID+" = "+
Long.toString(id), null);
c.moveToFirst();
String r = c.getString(0);
c.close();
db.close();
return r;
}
MyLocations.java中的一些方法:
private void get_MPNames() {
int[] x = mpoh.getIDs();
str = new String[x.length];
for (int i=0; i < x.length; i++) {
str[i] = mpoh.getMP_Name(x[i]);
}
Toast.makeText(this, getRowsNum()+" names left in DB", Toast.LENGTH_SHORT).show();
}
private void deleteMPfromListView(int pos) {
al.remove(pos);
adapter.notifyDataSetChanged();
Toast.makeText(getBaseContext(), al.size()+" rows left in list view", Toast.LENGTH_SHORT).show();
}
private void deleteMPFromDB(int pos) {
int []x = mpoh.getIDs();
mpoh.deleteMP(x[pos]);
Toast.makeText(getBaseContext(), getRowsNum()+" rows left in DB", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:-1)
尝试从db.close()
删除c.close()
和SQLiteOpenHelper
,并在每个startManagingCursor(c)
后使用rawQuery
。即使您在LogCat中收到警告,这也应该有效。关闭onDestroy中的数据库以删除警告。