2012-09-16 19 views
0

我工作的大樓目錄應用程序,並做一個地址反向查找時,該代碼將正常工作的第5條記錄,但隨着應用程序強制關閉,如果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

顯示該崩潰的logcat –

+0

從您發佈的例外情況看,它看起來不像,它適用於前5項。例外很明顯,遊標有0個項目,儘管看着你的代碼,這個錯誤不應該出現,因爲所有的錯誤檢查都完成了。因此,做一些挖掘後發佈整個異常堆棧 – nandeesh

+0

好,我發現問題是什麼。當輸入地址時,建築物的地址是建築物的名稱,並且BuildingDetails活動的sql語句遍歷名稱列而不是地址列。我用相同的代碼創建了一個新的活動(除了遍歷地址列外),並且所有內容都很好。感謝你的幫助。 – dynamite

回答

0

我認爲這個問題是你要關閉遊標! 嘗試,而不是讓活動管理你的光標通過添加

startManagingCursor(c); 

onCreate()方法。