0

我的应用程序由 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)
4

3 回答 3

1

This code is wrong (aside from lack of brackets I'd additionally punish you for):

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

The fact the cursor is not null does NOT mean it holds any data to fetch. So query can be successful (and cursor not null), but at the same time there can be zero results of that query. So before you start reading the results you need to ensure that there's data to harvest. The simplest approach would be to check if moveToFirst() returned true or call getCount() to get the number of results. See docs on Cursor

于 2013-07-05T11:45:29.060 回答
0

愿这能帮助你..

我想你还没有初始化 mDbhelper;

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

并且if (cursor != null)是错误的

于 2013-07-05T11:42:40.487 回答
-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;
            }
    }
于 2014-09-10T20:22:42.153 回答