如何解决android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0

时间:2013-07-05 11:35:48

标签: android

我的应用程序包含2个autoCompletetextview(ACT),当我点击列表中的一个项目时显示。我收到了这个错误。我试图解决它,但从昨天起,我仍然无法解决它。我真的希望你能帮助我或提出任何解决这个问题的建议。

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)

3 个答案:

答案 0 :(得分:1)

这段代码错了(除了缺少括号,我还要惩罚你):

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

光标不是null的事实 NOT 意味着它保存了要获取的任何数据。因此查询可以成功(并且游标不为空),但同时该查询的结果可能为零。因此,在开始阅读结果之前,您需要确保收集数据。最简单的方法是检查moveToFirst()是否返回true或致电getCount()以获取结果数量。见docs on Cursor

答案 1 :(得分:0)

这可以帮助你..

我认为你没有初学mDbhelper;

这样做: AutoCompleteDbAdapter mDbHelper = new AutoCompleteDbAdapter(context);

if (cursor != null)错误

答案 2 :(得分:-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;
            }
    }