Android 尝试重新打开已关闭的对象
当我开始运行我的应用程序时,我试图重新打开一个已经关闭的对象。出于某种原因,它正在buildCourseList方法中爆炸,但直到方法调用的最后,我才关闭数据库连接。知道我做错了什么吗?谢谢Android 尝试重新打开已关闭的对象,android,Android,当我开始运行我的应用程序时,我试图重新打开一个已经关闭的对象。出于某种原因,它正在buildCourseList方法中爆炸,但直到方法调用的最后,我才关闭数据库连接。知道我做错了什么吗?谢谢 public class MainActivity extends ActionBarActivity { //Create objects to match to UI views in the main activity ImageButton addCourseButton; ListView cr
public class MainActivity extends ActionBarActivity {
//Create objects to match to UI views in the main activity
ImageButton addCourseButton;
ListView createdList;
DBAdapter myDBAdapter = new DBAdapter(this);
/** Will be used to create an ArrayList to hold the data in */
ArrayList<String> listToHoldValues= new ArrayList<String>();
ArrayList<String> assignmentList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
addCourseButton = (ImageButton)findViewById(R.id.btnAddCourse);
createdList = (ListView)findViewById(R.id.listHolder);
/*Create default list on create, which is the course list*/
buildCourseList();
setListViewItems();
}
/*************** Insertion Methods ************************************************************/
public void insertNewCourse(String newCourse){
myDBAdapter.open();
long id;
id = myDBAdapter.insertCourse(newCourse);
myDBAdapter.close();
}
public void insertNewCategory(String newCategory, String associatedCourse){
myDBAdapter.open();
long id;
id= myDBAdapter.insertCategory(newCategory, associatedCourse);
myDBAdapter.close();
}
public void insertNewGrade(
int newGrade, String newAssignment, String associatedCourse, String associatedCategory){
myDBAdapter.open();
long id;
id = myDBAdapter.insertGrade(newGrade, newAssignment, associatedCourse, associatedCategory);
myDBAdapter.close();
}
/*************** Display Methods **************************************************************/
/* Will take the items from the Course table and insert them into an array*/
public void buildCourseList(){
myDBAdapter.open();
listToHoldValues.clear(); // Clear list to ensure that garbage isn't produced
/*Provides access to the data in the database*/
Cursor courseCursor = myDBAdapter.getAllCourses();
/*Loops through the data in the course table to populate a list of courses to return to UI*/
if(courseCursor.moveToFirst()){
do{
listToHoldValues.add(courseCursor.getString(1));
}while (courseCursor.moveToNext());
}
myDBAdapter.close();
}
/* Will take the items from the Category table and insert them into an array*/
public void buildCategoryList(){
myDBAdapter.open();
listToHoldValues.clear(); // Clear list to ensure that garbage isn't produced
/*Provides access to data in the database*/
Cursor categoryCursor = myDBAdapter.getAllCategories();
/*Loops through data in the category table to populate list of categories to return to ui*/
if(categoryCursor.moveToFirst()){
do {
listToHoldValues.add(categoryCursor.getString(1));
}while (categoryCursor.moveToNext());
}
myDBAdapter.close();
}
/* Will take the items from the Grade table and insert them into an array*/
public void buildGradeList(){
myDBAdapter.open();
listToHoldValues.clear();
/*Provides access to the data in the database*/
Cursor gradeCursor = myDBAdapter.getAllGrades();
/*Loops through the data to populate list of assignments and grades to return to the UI*/
if(gradeCursor.moveToFirst()){
do {
listToHoldValues.add(gradeCursor.getString(1));
assignmentList.add(gradeCursor.getString(2));
}while (gradeCursor.moveToNext());
}
myDBAdapter.close();
}
/**********************************Deletion Methods********************************************/
/*Will delete item from Course table depending course name passed in*/
public void deleteACourse(String course) {
myDBAdapter.open();
if (myDBAdapter.deleteCourse(course)) {
Toast.makeText(this, course + " deleted!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Delete failed", Toast.LENGTH_LONG).show();
}
myDBAdapter.close();
}
/*Will delete item from Category table depending on the category name passed in*/
public void deleteACategory(String category){
myDBAdapter.open();
if(myDBAdapter.deleteCategory(category)){
Toast.makeText(this, category + " deleted!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Delete failed!", Toast.LENGTH_LONG).show();
}
myDBAdapter.close();
}
/*Will delete item from grade table depending in assignment name passed in*/
public void deleteAGrade(String assignment){
myDBAdapter.open();
if (myDBAdapter.deleteCategory(assignment)){
Toast.makeText(this, assignment + " deleted!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Delete failed!", Toast.LENGTH_LONG).show();
}
myDBAdapter.close();
}
/********************************** Update Methods ********************************************/
/*Will update requested course to new value*/
public void updateACourse(String courseToUpdate, String name){
myDBAdapter.open();
if(myDBAdapter.updateCourse(courseToUpdate, name)){
Toast.makeText(this, "Update successful!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Update failed!", Toast.LENGTH_LONG).show();
}
myDBAdapter.close();
}
/*Will update requested category to new value*/
public void updateACategory(
String categoryToUpdate, String courseCategoryIsIn, String updateName){
myDBAdapter.open();
if(myDBAdapter.updateCategory(categoryToUpdate, courseCategoryIsIn, updateName)){
Toast.makeText(this, "Update successful!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Update failed!", Toast.LENGTH_LONG).show();
}
myDBAdapter.close();
}
/*Will updated a grade attached to an assignment to a new value*/
public void updateAGrade(String newGrade, String assignmentGradeAttachedTo,
String associatedCategory, String associatedCourse){
myDBAdapter.open();
if(myDBAdapter.updateGrade(newGrade, assignmentGradeAttachedTo,
associatedCategory, associatedCourse)){
Toast.makeText(this, "Update successful!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Update failed!", Toast.LENGTH_LONG).show();
}
myDBAdapter.close();
}
public double getCategoryAvg(){
double average = 0.0;
return average;
}
public double getCourseAverage(){
double average = 0.0;
return average;
}
/*Creates a ArrayAdapter to pass values to the list*/
public void setListViewItems(){
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, listToHoldValues);
// Set Adapter
createdList.setAdapter(listAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这就是logcat在爆炸时给我的信息:
*07-05 16:34:44.770 1027-1027/com.example.gradetracker.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.gradetracker.app, PID: 1027
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gradetracker.app/com.example.gradetracker.app.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.gradetracker.app/databases/MyGrades
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.gradetracker.app/databases/MyGrades
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:520)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:263)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.gradetracker.app.DBAdapter.open(DBAdapter.java:100)
at com.example.gradetracker.app.MainActivity.buildCourseList(MainActivity.java:68)
at com.example.gradetracker.app.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)*
为什么在
DatabaseHelper
中的onCreate()
方法之后调用db.close?
你不需要那个
始终确保您没有连续两次调用open()
或close()
方法。通过对SQLDatabase
对象调用isOpen()
,可以检查数据库是否已打开
请检查以了解有关此问题的更多信息。您可以显示堆栈跟踪吗?你的错误是什么?什么都看不见。用它编辑您的问题。我更改了它并编辑了主要帖子,logcat消息太长,无法用作评论,对此表示抱歉。为什么在DatabaseHelper中的onCreate()方法之后调用db.close?确保您没有连续两次调用open()或close()。通过对SQLDatabase对象调用isOpen(),可以检查数据库是否已打开。有关详细信息,请参阅。
*07-05 16:34:44.770 1027-1027/com.example.gradetracker.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.gradetracker.app, PID: 1027
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gradetracker.app/com.example.gradetracker.app.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.gradetracker.app/databases/MyGrades
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.gradetracker.app/databases/MyGrades
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:520)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:263)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.gradetracker.app.DBAdapter.open(DBAdapter.java:100)
at com.example.gradetracker.app.MainActivity.buildCourseList(MainActivity.java:68)
at com.example.gradetracker.app.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)*