如果sqlite记录超过5,则关闭应用程序

时间:2012-09-16 09:05:59

标签: android sqlite

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

1 个答案:

答案 0 :(得分:0)

我认为问题在于你关闭光标! 请尝试通过添加

让Activity管理光标
startManagingCursor(c);

到您的onCreate()方法。