Android返回光标

时间:2014-06-01 14:50:12

标签: java android

public Cursor DB_GetUnitPrice(){
    DataBaseHelper myDbHelper = new DataBaseHelper(this.getApplicationContext());
    myDbHelper = new DataBaseHelper(this);
    try { 
    myDbHelper.createDataBase();  
    } catch (IOException ioe) { 
    throw new Error("Unable to create database"); 
    }  
    try {  
    myDbHelper.openDataBase();         
    }catch(SQLException sqle){  
    throw sqle;  
    }

    try {  
    SQLiteDatabase db = myDbHelper.getReadableDatabase();

    String Insert_Data="Select Normal_Rate,Discounted_Rate FROM Tbl_Rate WHERE Route_ID="+global.Route_ID+" AND ((From_LocationID="+global.From_LocationID+" AND To_LocationID="+global.To_LocationID+") OR (From_LocationID="+global.To_LocationID+" AND To_LocationID="+global.From_LocationID + "))";
    Cursor c = db.rawQuery(Insert_Data, null);

    //c.close(); // cannot will error
    //db.close(); // cannot will error
    //myDbHelper.close();
        return c;
    }catch(Exception e){  
    Log.d("DB_GetUnitPrice",e.getMessage());
    }
    return null;
}

这是我如何返回Cursor,但问题是我无法关闭c.close();,因为因为关闭游标连接,会发生错误。如何在返回函数值之前关闭游标连接?

这是错误: -

    06-02 03:19:23.084: D/AndroidRuntime(6864): Shutting down VM
06-02 03:19:23.084: W/dalvikvm(6864): threadid=1: thread exiting with uncaught exception (group=0x40b3d1f8)
06-02 03:19:23.089: E/AndroidRuntime(6864): FATAL EXCEPTION: main
06-02 03:19:23.089: E/AndroidRuntime(6864): java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = Select Normal_Rate,Discounted_Rate FROM Tbl_Rate WHERE Route_ID=1 AND ((From_LocationID=1 AND To_LocationID=10) OR (From_LocationID=10 AND To_LocationID=1))) 
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:33)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:82)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:161)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.database.AbstractCursor.moveToNext(AbstractCursor.java:209)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at com.example.abc2.MainActivity.CheckUnitPrice(MainActivity.java:1794)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at com.example.abc2.MainActivity$3.onClick(MainActivity.java:543)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.view.View.performClick(View.java:3511)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.view.View$PerformClick.run(View.java:14105)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.os.Handler.handleCallback(Handler.java:605)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.os.Looper.loop(Looper.java:137)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at java.lang.reflect.Method.invokeNative(Native Method)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at java.lang.reflect.Method.invoke(Method.java:511)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-02 03:19:23.089: E/AndroidRuntime(6864):     at dalvik.system.NativeStart.main(Native Method)

来自Giru Bhai的答案

Cursor c = DB_GetUnitPrice(); c.close();

这是正确的方法吗?

1 个答案:

答案 0 :(得分:1)

来自Android文档。

close()

Closes the Cursor, releasing all of its resources and making it completely invalid

关闭后,您对return c;的来电可能会导致错误

可以尝试这样的事情。

Cursor c = DB_GetUnitPrice();

使用此光标对象,然后将光标关闭为

c.close();

有关光标的更多信息,请参阅此链接Android Cursor
修改
您无法重新打开关闭的对象,因为您在关闭它后尝试获取光标,因此您得到了错误