0

我正在开发一个构建目录应用程序,在进行地址反向查找时,代码对于前 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

}}

有什么想法吗?谢谢

编辑:继承人的崩溃日志:

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
4

1 回答 1

0

我认为问题在于您关闭了光标!尝试通过添加让 Activity 管理您的光标

startManagingCursor(c);

到你的onCreate()方法。

于 2012-09-16T09:11:55.490 回答