gpt4 book ai didi

java - 光标完成。关闭()问题

转载 作者:行者123 更新时间:2023-12-01 14:53:06 25 4
gpt4 key购买 nike

查询数据库后,我尝试通过游标对象从数据库中检索数据。我的cursor.close() 和关闭数据库时似乎出现错误。到目前为止这一切都很好,但当我从不同的 Intent 访问类时,我开始收到“光标终结器”错误。

目前,我的光标对象似乎以错误的方式关闭。

以下是我如何尝试检索所有数据以填充类的 onCreate 方法中的 ListView 的示例。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.appointmentview);


searchedAppView = (ListView)findViewById(android.R.id.list);

DBHandlerApp DBAppointments = new DBHandlerApp(this, null, null);

DBHandlerApp searchApps = new DBHandlerApp(this, null, null);

searchApps.open();


Cursor cursor = searchApps.getAppointmentsData();

startManagingCursor(cursor);


@SuppressWarnings("static-access")
String [] from = new String [] {DBAppointments.KEY_NAMEAPP, DBAppointments.KEY_TYPEAPP, DBAppointments.KEY_TIMEAPP, DBAppointments.KEY_DATEAPP, DBAppointments.KEY_COMMENTAPP};
int [] to = new int [] {R.id.txtAppointName, R.id.txtAppointType, R.id.txtAppointTime, R.id.txtAppointDate, R.id.txtAppointCom};

@SuppressWarnings("deprecation")

SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.setappointviews, cursor, from, to);
searchedAppView.setAdapter(cursorAdapter);



searchAppoints = (ImageButton) findViewById(R.id.btnSearchAppointName);
searchAppName = (EditText) findViewById(R.id.inputAppointName);


searchAppoints.setOnClickListener(this);

}

这是 DBHandlerApp 类的方法“getAppointmentData”:

    public Cursor getAppointmentsData() {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, null, null, null, null, KEY_NAMEAPP + " ASC", null);

//*****Closing the cursor object.*****
c.close();
return c;

Logcat 错误:

01-31 17:44:34.220: E/AndroidRuntime(269): FATAL EXCEPTION: main
01-31 17:44:34.220: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.ViewAppointments}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.os.Looper.loop(Looper.java:123)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 17:44:34.220: E/AndroidRuntime(269): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 17:44:34.220: E/AndroidRuntime(269): at java.lang.reflect.Method.invoke(Method.java:521)
01-31 17:44:34.220: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 17:44:34.220: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 17:44:34.220: E/AndroidRuntime(269): at dalvik.system.NativeStart.main(Native Method)
01-31 17:44:34.220: E/AndroidRuntime(269): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.support.v4.widget.CursorAdapter.getCount(CursorAdapter.java:202)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.widget.ListView.setAdapter(ListView.java:436)
01-31 17:44:34.220: E/AndroidRuntime(269): at com.example.flybase2.ViewAppointments.onCreate(ViewAppointments.java:54)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-31 17:44:34.220: E/AndroidRuntime(269): ... 11 more

目前我不使用 .searchApps.close(); 关闭数据库。如果这样做,我会收到以下 logcat 错误:

01-31 17:52:17.324: E/AndroidRuntime(277): FATAL EXCEPTION: main
01-31 17:52:17.324: E/AndroidRuntime(277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.ViewAppointments}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.os.Looper.loop(Looper.java:123)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 17:52:17.324: E/AndroidRuntime(277): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 17:52:17.324: E/AndroidRuntime(277): at java.lang.reflect.Method.invoke(Method.java:521)
01-31 17:52:17.324: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 17:52:17.324: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 17:52:17.324: E/AndroidRuntime(277): at dalvik.system.NativeStart.main(Native Method)
01-31 17:52:17.324: E/AndroidRuntime(277): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.support.v4.widget.CursorAdapter.getCount(CursorAdapter.java:202)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.widget.ListView.setAdapter(ListView.java:436)
01-31 17:52:17.324: E/AndroidRuntime(277): at com.example.flybase2.ViewAppointments.onCreate(ViewAppointments.java:56)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-31 17:52:17.324: E/AndroidRuntime(277): ... 11 more

最佳答案

您在错误的位置关闭了光标。您应该在完全使用完游标后关闭它,在填充游标后立即关闭它,然后再从 getAppointmentData 方法返回它。

只需从该方法中删除 close 调用即可解决您的问题,因为当您使用 startManagingCursor 时, Activity 会处理关闭它。

顺便说一句,startManagingCursor 已被贬值,转而使用 Loaders .

关于java - 光标完成。关闭()问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14632383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com