我目前正在通过一个学校项目,试图创建一个Scout Group经理。这是我第一次使用Java,XML或Android Studio,因此进度有些慢,但我已经到达了。我目前遇到的问题是从数据库中提取侦察员详细信息,以便将其放置在微调器中。领导者然后将从微调器中按名称选择要邀请的侦察员。显然是导致错误的代码(在数据库处理程序类中)如下:
public Cursor GetAllScouts()
{
String whereClause = COLUMN_SECTION + "=?";
String[] whereArgs = new String[]{"Beavers","Cubs","Scouts","Explorers","Network","Leaders"};
String[] columns = new String[]{COLUMN_SCID + " AS " + BaseColumns._ID, COLUMN_FNAME, COLUMN_LNAME, COLUMN_SECTION};
return db.query(TABLE_SCOUTS,columns,whereClause,whereArgs,null,null,null);//Error is being thrown up here
}
来自logcat的信息又在下面:
01-24 18:39:38.228 18179-18179/com.example.atomi.scoutmanagerprototype E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.atomi.scoutmanagerprototype, PID: 18179
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.atomi.scoutmanagerprototype/com.example.atomi.scoutmanagerprototype.frmCreateEvent2}: java.lang.IllegalArgumentException: Cannot bind argument at index 6 because the index is out of range. The statement has 1 parameters.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
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.IllegalArgumentException: Cannot bind argument at index 6 because the index is out of range. The statement has 1 parameters.
at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.example.atomi.scoutmanagerprototype.databaseHandler.GetAllScouts(databaseHandler.java:395)
at com.example.atomi.scoutmanagerprototype.frmCreateEvent2.loadSpinnerData(frmCreateEvent2.java:87)
at com.example.atomi.scoutmanagerprototype.frmCreateEvent2.onCreate(frmCreateEvent2.java:82)
at android.app.Activity.performCreate(Activity.java:5953)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
任何帮助将不胜感激。预先感谢。
答案 0 :(得分:2)
您的<form action='...'>
有一个参数。但是您正在whereClause = COLUMN_SECTION + "=?";
中传递6个选择参数。
这正是错误所暗示的。它期望单个whereArgument。
答案 1 :(得分:2)
您应该签出this answer。您的选择(where子句)必须包含与选择参数中给出的参数一样多的?
。在这种情况下,您的选择仅包含一个?
,但您提供6个选择参数。
您可能想要的选择是:
COLUMN_SECTION + "IN (?, ?, ?, ?, ?, ?)"
然后将您的部分列表作为selectionArgs传递。
考虑签出this tutorial,以了解有关使用SQLite保存数据的更多信息。
答案 2 :(得分:0)
该列表是否需要可变?如果没有,则只需在资源中的strings.xml
文件中指定一个字符串数组,如下所示:
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="scout_categories">
<item>Beavers</item>
<item>Cubs</item>
<item>Scouts</item>
<item>Explorers</item>
<item>Network</item>
<item>Leaders</item>
</string-array>
</resources>
然后,无论您在何处设置Spinner
对象,
SomeActivity.java
Spinner spinner=(Spinner)findViewById(R.id.spinner);
String[] scoutCategories = getResources().getStringArray(R.array.scout_categories);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, countries);
spinner.setAdapter(adapter);
*有关使用ArrayAdapter
的信息,请参见here
现在,如果确实需要一个可变列表,那么我建议您使用Android的Room Persistence Library。可以轻松地启动和运行数据库,并为您处理许多样板代码。