2013-07-05 69 views
0

我的應用程序包含2 autoCompletetextview(ACT),當我點擊列表中的項目之一顯示。我有這個錯誤。我試圖修復它,但從昨天開始,我仍然無法解決它。我真的希望你能幫助我或者給出任何建議來解決這個問題。如何解決android.database.CursorIndexOutOfBoundsException:索引0請求,大小爲0

FindMEPlace.java

public class FindMePlace extends Activity { 

    public static UkmRoute selectedPath = null; 
    private AutoCompleteDbAdapter mDbHelper; 
    public AutoCompleteTextView fromLocation, toDestination; 
    Button search; 
    ArrayAdapter<String> arrayAdapter1 = null; 
    ArrayAdapter<String> arrayAdapter2 = null; 
    final ArrayList<String> results = new ArrayList<String>(); 
    final ArrayList<String> results_id = new ArrayList<String>(); 
    final ArrayList<String> results2 = new ArrayList<String>(); 
    final ArrayList<String> results_id2 = new ArrayList<String>(); 
    final AutoCompleteDbAdapter dbHelper = new AutoCompleteDbAdapter(this); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.find_me_place); 

     dbHelper.open(); 

     //link with the layout items 
     fromLocation = (AutoCompleteTextView) findViewById(R.id.locationTxt); 
     toDestination = (AutoCompleteTextView) findViewById(R.id.destinationTxt); 
     search = (Button) findViewById(R.id.button1); 

     //--------------------------------------------------------------------- 
     //--------------------------------------------------------------------- 

     //---------------------LOCATION-------------------------------------- 
     // Reading location 
     Log.d("Reading", "Reading all location.."); 
     List<Location> location = dbHelper.getAllLocation(); 
     for (Location k : location) { 
      results.add(k.getLocationUkm()); 
      results_id.add(k.getID()); 
     } 
     arrayAdapter1 = new ArrayAdapter<String>(FindMePlace.this,R.layout.list_item, R.id.textView1, results); 
     fromLocation.setAdapter(arrayAdapter1); 

     fromLocation.setOnItemClickListener(new OnItemClickListener(){ 

      public void onItemClick(AdapterView<?> listView, View view, 
        int position, long id) { 

       // When clicked, show a toast with the TextView text 
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
         Toast.LENGTH_SHORT).show(); 

        Log.d("test", "position:" + position); 
        Log.d("test", "actualname:" + dbHelper.getSingleLocation(arrayAdapter1.getItem(position)).getLocationUkm()); 

       String fromLoc = dbHelper.getSingleLocation(arrayAdapter1.getItem(position)).getID(); 
       String name = arrayAdapter1.getItem(position); 
       fromLocation.setText(fromLoc); 

      } 
     }); 

誤差點這裏

Log.d("test", "actualname:" + dbHelper.getSingleLocation(arrayAdapter1.getItem(position)).getLocationUkm()); 

AutoCompleteDbAdapter.java

public void addLocation(Location location) { 
      mDb = mDbHelper.getReadableDatabase(); 

      ContentValues values = new ContentValues(); 
      values.put(KEY_LOCATIONID, location.getID()); // ID 
      values.put(KEY_LOCATION_NAME, location.getLocationUkm()); // from 

      // Inserting Row 
      mDb.insert(DATABASE_NAME, null, values); 
      mDb.close(); // Closing database connection 
     } 
     // Getting single Poi 
      public Location getSingleLocation(String id_location) { 
       mDb = mDbHelper.getReadableDatabase(); 

       Cursor cursor = mDb.query(TABLE_LOCATION, 
         new String[] {KEY_LOCATIONID, KEY_LOCATION_NAME}, 
         KEY_LOCATIONID + "=?", 
         new String[] {String.valueOf(id_location)}, null, null, null, null); 

       if (cursor != null) 
        cursor.moveToFirst(); 

       Location location = new Location(cursor.getString(0), 
         cursor.getString(1)); 
       return location; 
      } 


      // Getting single UKMRoute by From 
      public Location getLocationName(String Location_name) { 
       mDb = mDbHelper.getReadableDatabase(); 

       Cursor cursor = mDb.query(TABLE_LOCATION, 
         new String[] {KEY_LOCATIONID, KEY_LOCATION_NAME}, 
         KEY_LOCATION_NAME + "=?", 
         new String[] { String.valueOf(Location_name) }, null, null, null, null); 

       if (cursor != null) 
        cursor.moveToFirst(); 

       Location location = new Location(cursor.getString(0),cursor.getString(1)); 
       return location; 
      } 


      // Getting all Poi 
        public List<Location> getAllLocation() { 
         List<Location> locationList = new ArrayList<Location>(); 
         //Select All Query 
         String selectQuery = "SELECT * FROM " + TABLE_LOCATION; 

         mDb = mDbHelper.getReadableDatabase(); 
         Cursor cursor = mDb.rawQuery(selectQuery, null); 

         //looping through all rows and adding to list 
         if (cursor.moveToFirst()) { 
          do { 
           Location poi = new Location(); 
           poi.setID(cursor.getString(0)); 
           poi.setLocationUkm(cursor.getString(1)); 

           locationList.add(poi); 

          } while (cursor.moveToNext()); 
         } 

         //sorting list 
         Collections.sort(locationList,new Comparator<Location>() { 
          public int compare(Location poi, Location otherPoi) { 
           return poi.getID().compareTo(otherPoi.getID()); 
          } 
         }); 

         // return Poi list 
         mDb.close(); 
         return locationList; 
        } 

另一個錯誤是指向這裏

Location location = new Location(cursor.getString(0),cursor.getString(1));

07-05 11:32:58.183: E/AndroidRuntime(30311): FATAL EXCEPTION: main 
07-05 11:32:58.183: E/AndroidRuntime(30311): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at com.example.series1.AutoCompleteDbAdapter.getSingleLocation(AutoCompleteDbAdapter.java:452) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at com.example.series1.FindMePlace$1.onItemClick(FindMePlace.java:73) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:952) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.widget.AutoCompleteTextView.access$1400(AutoCompleteTextView.java:92) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1489) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.widget.ListView.performItemClick(ListView.java:3513) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.os.Handler.handleCallback(Handler.java:587) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.os.Handler.dispatchMessage(Handler.java:92) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.os.Looper.loop(Looper.java:123) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at android.app.ActivityThread.main(ActivityThread.java:3683) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at java.lang.reflect.Method.invokeNative(Native Method) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at java.lang.reflect.Method.invoke(Method.java:507) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
07-05 11:32:58.183: E/AndroidRuntime(30311): at dalvik.system.NativeStart.main(Native Method) 
+0

檢查文檔中返回什麼'Cursor.moveToFirst()'這意味着什麼... – Selvin

+0

您檢查(光標!= NULL),你應該與如果(光標做到這一點。 moveToFirst())。 – AlexBcn

+0

大小從1開始,從0開始索引...當大小爲0時,您不能請求索引0.索引0僅在大小大於或等於1時可用。 –

回答

0

願這幫助你..

我想你還沒有intialized mDbhelper;

這樣做: AutoCompleteDbAdapter mDbHelper = new AutoCompleteDbAdapter(context);

if (cursor != null)是錯誤的

+0

'if(cursor!= null)'如果這個錯誤,什麼是正確的需要使用? – riehime23

+0

使用if(cursor.getCount()> 0)而不是那 –

+0

我可以用這種方式嗎? //檢查遊標是否爲空,並確保遊標 //不爲空 if(cursor!= null && cursor.getCount()> 0){ if(cursor。moveToFirst())//循環直到它到達光標的末尾 do { //在這裏做某事 } while(cursor.moveToNext()); } //確保關閉遊標 cursor.close(); } – riehime23

1

這段代碼是錯誤的(因缺乏支架拋開我還懲罰你):

if (cursor != null) 
    cursor.moveToFirst(); 

的事實光標不是null確實不是表示它包含任何要取回的數據。因此,查詢可以成功(並且遊標不爲空),但同時該查詢的結果可能爲零。所以在你開始閱讀結果之前,你需要確保有數據要收穫。最簡單的方法是檢查moveToFirst()是否返回true或致電getCount()以獲得結果數量。見docs on Cursor

-1

我知道這是很晚,但我說如果對另一個人有這個問題。 試試這個代碼:

public boolean isempty(String tablename) 
    { 

      Cursor cur2 = database.rawQuery("SELECT COUNT(*) FROM " 
        + tablename , null); 

      if(cur2 != null) 
      { 
       Log.e("DATA", "CURSOR NOT NULL"); 
        if(cur2.moveToFirst()) 
        { 
         Log.e("DATA", "CURSOR MOVE TO FIRST"); 
         if(cur2.getInt(0) != 0) 
         { 
          Log.e("DATA", "CURSOR HAS INDEX"); 
          if(cur2.isClosed()) 
          { 
           cur2.close(); 
           return false; 
          } 
          else 
          { 
           cur2.close(); 
           return false; 
          } 
         } 
         else 
         { 
          Log.e("DATA", "CURSOR DON'T HAVE INDEX"); 
          if(cur2.isClosed()) 
          { 
           return true; 
          } 
          else 
          { 
           cur2.close(); 
           return true; 
          } 
         } 
        } 
        else 
        { 
         Log.e("DATA", "CURSOR MOVE TO FIRST IS NULL"); 
         if(cur2.isClosed()) 
          return true; 
         else 
         { 
          cur2.close(); 
          return true; 
         } 

        } 
      } 
      else 
      { 
       Log.e("DATA", "CURSOR IS NULL"); 
       return true; 
      } 
    } 
+1

有一些解釋或評論的答案通常比僅有代碼的答案更有幫助,特別是當有非常多的代碼時。 – skrrgwasme

相關問題