我目前正在计算机科学A级控制的评估中,正在构建一个旨在帮助领导者管理其侦察员小组的应用程序。这是我第一次使用Android Studio,Java,sqlite或XML进行工作,因此我遇到了很多问题,这是最近的一个问题。我正在尝试创建一个函数,该函数将从表中生成随机膳食,以便为某个事件创建膳食计划。下面,我将找到的代码放在databaseHandler类中:
//Select a random lunch
public Cursor getRandomMeal()
{
String whereClause = COLUMN_MTYPE + "=?";
String[] whereArgs = new String[]{"Lunch"};
String columns[] = new String[]{COLUMN_MEID + " AS " + BaseColumns._ID, COLUMN_MNAME, COLUMN_MTYPE, COLUMN_INGR, COLUMN_FCOST};
Cursor cursor = db.query(TABLE_FOOD, columns, whereClause, whereArgs, null, null, "RANDOM()limit 1");
return cursor;
}
紧随其后的是类的代码,该活动在其中被调用:
double TotalFoodCost;
if (providingfood=true)
{
Cursor FoodItem = FoodDatabaseHandler.getRandomMeal();
String FoodName = FoodItem.getString(FoodItem.getColumnIndex(FoodDatabaseHandler.COLUMN_MNAME));//error is apparently occurring here
String FoodIngredients = FoodItem.getString(FoodItem.getColumnIndex(FoodDatabaseHandler.COLUMN_INGR));
double FoodPricePerPerson = FoodItem.getDouble(FoodItem.getColumnIndex(FoodDatabaseHandler.COLUMN_FCOST));
TotalFoodCost = FoodPricePerPerson*(31-ScoutsRemaining);//31 due to the head leader being included
FoodDatabaseHandler.close();
}
else
{
TotalFoodCost=0;
}
最后,这是发生错误时LogCat显示的内容:
01-25 18:27:08.785 29383-29383/com.example.atomi.scoutmanagerprototype E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.atomi.scoutmanagerprototype, PID: 29383
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5310)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5310)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.atomi.scoutmanagerprototype.frmCreateEvent2.saveAndExit(frmCreateEvent2.java:154)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5310)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
我很努力,因为在阅读错误消息后似乎无法解决该问题-如果有人可以指出正确的方向,那就太好了。