如何修复 'database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0'?
How to fix 'database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0'?
我正在使用 Android SQLite 为我的 weight_activity 设置一个带有对话框的数据库。在那个 Activity 中,我保存了当前体重,我想在启动 activity 显示它时请求当前体重的保存值。
我得到的错误是:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thisfit/com.example.thisfit.Weight_Activity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
这是在我的 activity class
中请求当前体重的代码
public void getCurrentWeightAsFloat() {
textView_currentWeight.setText(String.valueOf(weight_dbHandler.getCurrentWeight()));
} //public void getCurrentWeightAsFloat()
这是整个数据库代码。我 post 这是因为我不知道问题是否出在代码的另一部分,而不仅仅是方法 getCurrentWeight():
public class Weight_Database extends SQLiteOpenHelper {
//region Attributes
private static final String TAG = "weight_database";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "weight_database.db";
private static final String TABLE_NAME = "weight_table";
private static final String COL1 = "ID";
private static final String COL2 = "Weight";
private static final String COL3 = "Date";
//endregion
//region Constructors
public Weight_Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// SQLiteDatabase db = this.getWritableDatabase();
}
//endregion
//region Methods
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME
+ " (ID INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL2 + " FLOAT, "
+ COL3 + " DATE )";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addNewCurrentWeightAsFloat(float newCurrentWeightAsFloat){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, newCurrentWeightAsFloat);
Log.d(TAG, "addWeight: Adding " + newCurrentWeightAsFloat + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1){
return false;
} else {
return true;
}
} //public boolean addNewCurrentWeightAsFloat(String newCurrentWeightAsFloat)
public float getCurrentWeight(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT Weight FROM " + TABLE_NAME + " ORDER BY ID DESC LIMIT 1";
Cursor data = db.rawQuery(query, null);
data.moveToFirst();
float currentWeightAsFloat = data.getFloat(data.getColumnIndex("content"));
return currentWeightAsFloat;
} //public float getCurrentWeight()
//Returns the whole table as a raw query
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
} //public Cursor getData()
//endregion
} //class Weight_Database
我希望通过选择限制为 1 的 ID DESCending 来获取输入的最新重量。
我想得到我当前体重的浮点数,然后在 textView 中显示它。
祝你有愉快的一天,提前致谢:)
您的table没有数据。
您应该检查 data.moveToFirst()
的 return 值,并且只尝试在游标上调用 getFloat()
以防 return 为真。
我正在使用 Android SQLite 为我的 weight_activity 设置一个带有对话框的数据库。在那个 Activity 中,我保存了当前体重,我想在启动 activity 显示它时请求当前体重的保存值。
我得到的错误是:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thisfit/com.example.thisfit.Weight_Activity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
这是在我的 activity class
中请求当前体重的代码 public void getCurrentWeightAsFloat() {
textView_currentWeight.setText(String.valueOf(weight_dbHandler.getCurrentWeight()));
} //public void getCurrentWeightAsFloat()
这是整个数据库代码。我 post 这是因为我不知道问题是否出在代码的另一部分,而不仅仅是方法 getCurrentWeight():
public class Weight_Database extends SQLiteOpenHelper {
//region Attributes
private static final String TAG = "weight_database";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "weight_database.db";
private static final String TABLE_NAME = "weight_table";
private static final String COL1 = "ID";
private static final String COL2 = "Weight";
private static final String COL3 = "Date";
//endregion
//region Constructors
public Weight_Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// SQLiteDatabase db = this.getWritableDatabase();
}
//endregion
//region Methods
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME
+ " (ID INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL2 + " FLOAT, "
+ COL3 + " DATE )";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addNewCurrentWeightAsFloat(float newCurrentWeightAsFloat){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, newCurrentWeightAsFloat);
Log.d(TAG, "addWeight: Adding " + newCurrentWeightAsFloat + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1){
return false;
} else {
return true;
}
} //public boolean addNewCurrentWeightAsFloat(String newCurrentWeightAsFloat)
public float getCurrentWeight(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT Weight FROM " + TABLE_NAME + " ORDER BY ID DESC LIMIT 1";
Cursor data = db.rawQuery(query, null);
data.moveToFirst();
float currentWeightAsFloat = data.getFloat(data.getColumnIndex("content"));
return currentWeightAsFloat;
} //public float getCurrentWeight()
//Returns the whole table as a raw query
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
} //public Cursor getData()
//endregion
} //class Weight_Database
我希望通过选择限制为 1 的 ID DESCending 来获取输入的最新重量。 我想得到我当前体重的浮点数,然后在 textView 中显示它。
祝你有愉快的一天,提前致谢:)
您的table没有数据。
您应该检查 data.moveToFirst()
的 return 值,并且只尝试在游标上调用 getFloat()
以防 return 为真。