从游标+ sqlite填充listview + CursorIndexOutOfBoundsException:请求索引-1,大小为0

时间:2015-01-31 18:03:47

标签: android sqlite android-listview cursor simplecursoradapter

我知道有关此问题的多个主题;但我无法解决CursorIndexOutOfBoundsException。 我正在尝试使用游标填充我的查询并将结果提取到listview.But每当调用第二个活动(用于创建列表视图)时,应用程序崩溃。在logcat中它说“CursorIndexOutOfBoundsException:索引-1请求,大小为0” 我正在附加代码段以便更好地理解。 DatabaseHandler.java

public class DatabaseHandler extends SQLiteAssetHelper {

    // for our logs
    public static final String TAG = "DatabaseHandler.java";

    // database version
    private static final int DATABASE_VERSION = 1;

    // database name
    protected static final String DB_NAME = "med2";

    public static String DB_PATH;

    // table details
    public String tableName = "allopathmedicine";

    public String _id = "_id";

    public String fieldObjectName = "medicinename";

    public String medtype = "medicinetype";

    public String companyname = "companyname";

    public String medgroup = "medicinegroup";

    public String medfor ="medicinefor";

    public String medid = "registrationid";

    public String p;

    private SQLiteDatabase database;

    private Context context;

    public long id2;

    // constructor
    public DatabaseHandler(Context context) throws IOException {
        super(context, DB_NAME, null, DATABASE_VERSION);
        this.context=context;
        this.database= getReadableDatabase();
    }

    /*
     * Read records related to the search term
     */
    public MyObject[] read(String searchTerm) {
        // select query
        String sql = "";
        sql += "SELECT * FROM " + tableName;
        sql += " WHERE " + fieldObjectName + " LIKE '" + searchTerm + "%'";
        sql += " ORDER BY " + _id + " DESC";
        sql += " LIMIT 0,15";

        SQLiteDatabase db = this.getWritableDatabase();

        // execute the query
        Cursor cursor = db.rawQuery(sql, null);

        int recCount = cursor.getCount();

        MyObject[] ObjectItemData = new MyObject[recCount];
        int x = 0;

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {

                String objectName = cursor.getString(cursor.getColumnIndex(fieldObjectName));
                id2 = cursor.getInt(cursor.getColumnIndexOrThrow(_id));
                Log.e(TAG, "objectName: " + objectName);

                MyObject myObject = new MyObject(objectName);

                ObjectItemData[x] = myObject;

                x++;

            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
         p= ObjectItemData.toString();
        return ObjectItemData;
    } 

    public Cursor get() throws IOException
    {
        return this.Getmedicine(id2);
    }
    public Cursor Getmedicine(long id2) throws SQLException {
         SQLiteDatabase db = this.getWritableDatabase(); 
        Cursor mCursor =
          db.query(true, tableName, new String[] {medid,_id,
              medfor, medtype,fieldObjectName}, _id + " = " + id2, null,
              null, null, null, null);
        String rid = mCursor.getString(mCursor.getColumnIndex(medid));
        mCursor.moveToFirst();
        if (mCursor != null) {
          mCursor.moveToFirst();
        }
        mCursor.close();
        db.close();
        return mCursor;
      }

    /*public ArrayList<Medicineinfo> getmedDetails(String ObjectItemData) throws SQLException {
        SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<Medicineinfo> getmedDetails = new ArrayList<Medicineinfo>();
        Cursor cursor =
            db.query(true,tableName, new String[] {
                        fieldObjectName,
                        medtype,
                        companyname,
                        medgroup},
                    fieldObjectName + "=?" ,
                    new String[] {ObjectItemData},
                    null, null, null , null);
        if (cursor.moveToFirst()) {
            do {
                Medicineinfo Medicineinfo= new Medicineinfo();
                Medicineinfo.setMedfor(cursor.getString(cursor.getColumnIndexOrThrow(medfor)));
                Medicineinfo.setMedid (cursor.getString(cursor.getColumnIndex(medid)));
                //Medicineinfo.comName( cursor.getString(cursor.getColumnIndex(companyname)));
                Medicineinfo.setMedgroup(cursor.getString(cursor.getColumnIndex(medgroup)));
                Medicineinfo.setMedtype(cursor.getString(cursor.getColumnIndex(medtype)));
                getmedDetails.add(Medicineinfo);
            } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return getmedDetails;
    }*/

}

Detailinformation.java

public class Detailinformation extends ListActivity {
    private Cursor mcursor;
    SimpleCursorAdapter adapter;

    // for database operations
    DatabaseHandler databaseH;

    @Override
    protected void onCreate(Bundle savedInstanceState) throws SQLException {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);


            // get data from cursor
          try {
                databaseH = new DatabaseHandler(Detailinformation.this);

            // put sample data to database
         mcursor = databaseH.get();
         mcursor.moveToFirst();

         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                    android.R.layout.simple_list_item_2, 
                    databaseH.get(), 
                    new String[] { "_id", "medid" }, 
                    new int[] { android.R.id.text1, android.R.id.text2 });
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          ListView lv = (ListView) findViewById(android.R.id.list);
         lv.setAdapter(adapter);
    }   

        //get data from arraylist


    protected void onDestroy() {
        super.onDestroy();
        mcursor.close();

    }
}

logcat的:

01-31 23:39:50.942: E/SQLiteLog(2629): (14) cannot open file at line 30046 of [9491ba7d73]
01-31 23:39:50.942: E/SQLiteLog(2629): (14) os_unix.c:30046: (2) open(/data/data/com.com.mity.medicalinformationbd/databases/med2) - 
01-31 23:39:50.948: I/art(2629): Background sticky concurrent mark sweep GC freed 1806(102KB) AllocSpace objects, 0(0B) LOS objects, 33% free, 412KB/623KB, paused 1.226ms total 135.625ms
01-31 23:39:50.995: E/SQLiteDatabase(2629): Failed to open database '/data/data/com.com.mity.medicalinformationbd/databases/med2'.
01-31 23:39:50.995: E/SQLiteDatabase(2629): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.returnDatabase(SQLiteAssetHelper.java:363)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:344)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:178)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:257)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at com.mity.medicalinformationbd.DatabaseHandler.<init>(DatabaseHandler.java:56)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at com.mity.medicalinformationbd.MainActivity.onCreate(MainActivity.java:43)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.app.Activity.performCreate(Activity.java:5933)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.os.Looper.loop(Looper.java:135)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at android.app.ActivityThread.main(ActivityThread.java:5221)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at java.lang.reflect.Method.invoke(Native Method)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at java.lang.reflect.Method.invoke(Method.java:372)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-31 23:39:50.995: E/SQLiteDatabase(2629):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-31 23:39:50.995: W/SQLiteAssetHelper(2629): could not open database med2 - unknown error (code 14): Could not open database
01-31 23:39:50.995: W/SQLiteAssetHelper(2629): copying database from assets...
01-31 23:39:51.102: W/SQLiteAssetHelper(2629): extracting file: 'med2.db'...
01-31 23:39:51.632: W/SQLiteAssetHelper(2629): database copy complete
01-31 23:39:51.667: I/art(2629): Background partial concurrent mark sweep GC freed 506(88KB) AllocSpace objects, 0(0B) LOS objects, 55% free, 411KB/923KB, paused 8.723ms total 35.226ms
01-31 23:39:51.691: I/SQLiteAssetHelper(2629): successfully opened database med2
01-31 23:39:52.624: D/gralloc_goldfish(2629): Emulator without GPU emulation detected.
01-31 23:48:16.207: I/Choreographer(2629): Skipped 130 frames!  The application may be doing too much work on its main thread.
01-31 23:48:17.672: E/CustomAutoCompleteTextChangedListener.java(2629): User input: a
01-31 23:48:17.949: E/DatabaseHandler.java(2629): objectName: Anestic Cream
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Alconil Mouthwash
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Albamax DS
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Analac
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Atolip 10
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avloxin 500
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avloxin
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avloxin 250
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avlotrin
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avlotrin
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avlotrin
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avlosef 500
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avlosef
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avlosef DS 250
01-31 23:48:17.950: E/DatabaseHandler.java(2629): objectName: Avlosef 500 mg
01-31 23:48:18.366: I/Choreographer(2629): Skipped 36 frames!  The application may be doing too much work on its main thread.
01-31 23:48:20.109: I/Choreographer(2629): Skipped 45 frames!  The application may be doing too much work on its main thread.
01-31 23:48:20.340: E/CustomAutoCompleteTextChangedListener.java(2629): User input: com.mity.medicalinformationbd.MyObject@26b3e612
01-31 23:48:20.410: I/SQLiteAssetHelper(2629): successfully opened database med2
01-31 23:48:20.555: E/CustomAutoCompleteTextChangedListener.java(2629): User input: Anestic Cream
01-31 23:48:20.575: I/SQLiteAssetHelper(2629): successfully opened database med2
01-31 23:48:20.619: E/DatabaseHandler.java(2629): objectName: Anestic Cream
01-31 23:48:20.766: I/Choreographer(2629): Skipped 150 frames!  The application may be doing too much work on its main thread.
01-31 23:48:21.846: I/SQLiteAssetHelper(2629): successfully opened database med2
01-31 23:48:22.099: D/AndroidRuntime(2629): Shutting down VM
01-31 23:48:22.099: D/AndroidRuntime(2629): --------- beginning of crash
01-31 23:48:22.309: E/AndroidRuntime(2629): FATAL EXCEPTION: main
01-31 23:48:22.309: E/AndroidRuntime(2629): Process: com.com.mity.medicalinformationbd, PID: 2629
01-31 23:48:22.309: E/AndroidRuntime(2629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.com.mity.medicalinformationbd/com.mity.medicalinformationbd.Detailinformation}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.os.Looper.loop(Looper.java:135)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.app.ActivityThread.main(ActivityThread.java:5221)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at java.lang.reflect.Method.invoke(Native Method)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at java.lang.reflect.Method.invoke(Method.java:372)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-31 23:48:22.309: E/AndroidRuntime(2629): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at com.mity.medicalinformationbd.DatabaseHandler.Getmedicine(DatabaseHandler.java:122)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at com.mity.medicalinformationbd.DatabaseHandler.get(DatabaseHandler.java:113)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at com.mity.medicalinformationbd.Detailinformation.onCreate(Detailinformation.java:56)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.app.Activity.performCreate(Activity.java:5933)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-31 23:48:22.309: E/AndroidRuntime(2629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
01-31 23:48:22.309: E/AndroidRuntime(2629):     ... 10 more
01-31 23:48:22.370: I/art(2629): Background sticky concurrent mark sweep GC freed 2136(154KB) AllocSpace objects, 0(0B) LOS objects, 24% free, 692KB/923KB, paused 1.806ms total 213.658ms
01-31 23:48:25.600: I/Process(2629): Sending signal. PID: 2629 SIG: 9

0 个答案:

没有答案