ディレクトリアプリの構築に取り組んでおり、アドレスの逆引きを行うと、コードは最初の 5 つのレコードに対しては正常に機能しますが、次のエラーでクラッシュします。
「android.database.CursorIndexOutOfBoundsException: サイズ 0 のインデックス 0 が要求されました」
レコード数と列インデックスを記録しました。テーブルの最初の 5 つのレコードは期待どおりの結果を生成しますが、インデックス カウントがそれを超えると、クラッシュします。
アクティビティのコードは次のとおりです。
public class AddressReverse extends Activity implements TextWatcher
{
AutoCompleteTextView actv;
String address[];
private ArrayList<String> results = new ArrayList<String>();
private SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.address_reverse);
DatabaseHelper helper = new DatabaseHelper(this.getApplicationContext());
db = helper.getWritableDatabase();
Cursor c = db.rawQuery("SELECT address FROM buildings", null);
if (c!=null)
{
if (c.moveToFirst())
{
do
{
String address = c.getString(c.getColumnIndex("address"));
results.add(address);
Log.d("Cursor count", c.getCount()+"");
Log.d("Cursor Index", c.getColumnIndex("address")+"");
}
while (c.moveToNext());
}
c.close();
}
actv = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
actv.addTextChangedListener(this);
actv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, results));
actv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String address = ((TextView)view).getText().toString();
Intent i =new Intent(getApplicationContext(), BuildingDetails.class);
i.putExtra("building", address);
startActivity(i);
}
});
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}}
何かご意見は?ありがとう
編集: クラッシュの logcat は次のとおりです。
E/AndroidRuntime( 8952): FATAL EXCEPTION: main
E/AndroidRuntime( 8952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dynamite.bible/com.dynamite.bible.BuildingDetails}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
E/AndroidRuntime( 8952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2182)
E/AndroidRuntime( 8952): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2207)
E/AndroidRuntime( 8952): at android.app.ActivityThread.access$600(ActivityThread.java:139)
E/AndroidRuntime( 8952): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
E/AndroidRuntime( 8952): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 8952): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 8952): at android.app.ActivityThread.main(ActivityThread.java:4899)
E/AndroidRuntime( 8952): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 8952): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 8952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 8952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
E/AndroidRuntime( 8952): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 8952): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
E/AndroidRuntime( 8952): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
E/AndroidRuntime( 8952): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
E/AndroidRuntime( 8952): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
E/AndroidRuntime( 8952): at com.dynamite.bible.BuildingDetails.onCreate(BuildingDetails.java:40)
E/AndroidRuntime( 8952): at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime( 8952): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime( 8952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
E/AndroidRuntime( 8952): ... 11 more
W/ActivityManager( 618): Force finishing activity com.dynamite.bible/.BuildingDetails
W/ActivityManager( 618): Force finishing activity com.dynamite.bible/.AddressReverse