Kyle Osborne Kyle Osborne - 3 months ago 18
SQL Question

Android Studio - attempt to re-open an already-closed object: SQLiteDatabase

I am making my first Android app and I am a complete novice when it comes to working with Java as I have only ever used Python. I am trying to create a database that stores the user's details through an initial set up activity, however I am getting an error

08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/fitness.essentials.fitnessessentials/databases/UserData.db
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:525)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:262)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at fitness.essentials.fitnessessentials.Database.<init>(Database.java:26)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at fitness.essentials.fitnessessentials.SetupScreen.onCreate(SetupScreen.java:44)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6876)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:158) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7224) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 


Here is the database class main bits

// Constructor method
public Database(Context context) {
// Superclass constructor called to create database
super(context, DATABASE_NAME, null, DATABASE_VERSION);
onCreate(this.getWritableDatabase());
}

// On create the databases are created (if they don't exist)
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_USER);
db.close();
}

// Currently no special treatment for upgrading or downgrading the schema
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { }


/* Shorthand methods for generic tasks */
// Add a single row of user data
public long addUserData(String name, String gender, String dob, String height, float currentweightNum,
float targetweightNum, String activitylevel, String weightdifference) {
// Get database that is readable
SQLiteDatabase db = this.getWritableDatabase();

// Define row values
ContentValues values = new ContentValues();
values.put(DataContract.UserColumns.COLUMN_NAME, name);
values.put(DataContract.UserColumns.COLUMN_GENDER, gender);
values.put(DataContract.UserColumns.COLUMN_DOB, dob);
values.put(DataContract.UserColumns.COLUMN_HEIGHT, height);
values.put(DataContract.UserColumns.COLUMN_CURRENT_WEIGHT, currentweightNum);
values.put(DataContract.UserColumns.COLUMN_TARGET_WEIGHT, targetweightNum);
values.put(DataContract.UserColumns.COLUMN_ACTIVITY_LEVEL, activitylevel);
values.put(DataContract.UserColumns.COLUMN_WEIGHT_DIFFERENCE, weightdifference);


// Insert values into the database, in the user data table
long newRowId = db.insert(
DataContract.UserColumns.TABLE_NAME,
null,
values);

db.close();
// Return the row id that the data was inserted into
return newRowId;
}


And here is the setup screen class that I am trying to add the details from

public class SetupScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

private String Name;
private String Gender;
private String Height;
private String CurrentWeight;
private String TargetWeight;
private String DOB;
private String ActivityLevel;
private String WeightDifference;

private Database dbhelper;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup_screen);
Name = "";
Gender = "";
Height = "";
CurrentWeight = "";
TargetWeight = "";
DOB = "";
ActivityLevel = "";
WeightDifference = "";


dbhelper = new Database(getApplicationContext());





Spinner spinnerGender = (Spinner) findViewById(R.id.spinnerGender);

ArrayAdapter<CharSequence> adapterGender = ArrayAdapter.createFromResource(this,
R.array.gender, android.R.layout.simple_spinner_item);
adapterGender.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGender.setAdapter(adapterGender);

spinnerGender.setOnItemSelectedListener(this);

Spinner spinnerActivityLevel = (Spinner) findViewById(R.id.spinnerActivityLevel);

ArrayAdapter<CharSequence> adapterActivity = ArrayAdapter.createFromResource(this,
R.array.activity_level, android.R.layout.simple_spinner_item);
adapterActivity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerActivityLevel.setAdapter(adapterActivity);

spinnerActivityLevel.setOnItemSelectedListener(this);

Spinner spinnerWeightDifference = (Spinner) findViewById(R.id.spinnerWeightDifference);

ArrayAdapter<CharSequence> adapterWeight = ArrayAdapter.createFromResource(this,
R.array.weight_difference, android.R.layout.simple_spinner_item);
adapterWeight.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerWeightDifference.setAdapter(adapterWeight);

spinnerWeightDifference.setOnItemSelectedListener(this);



}

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Gender = parent.getItemAtPosition(position).toString();
ActivityLevel = parent.getItemAtPosition(position).toString();
WeightDifference = parent.getItemAtPosition(position).toString();
}

public void onNothingSelected(AdapterView<?> parent) {
// An interface callback
}

public void save(View view) {


Name = ((EditText)findViewById(R.id.Name)).getText().toString();
DOB = ((EditText)findViewById(R.id.DOB)).getText().toString();
Height = ((EditText)findViewById(R.id.Height)).getText().toString();
CurrentWeight = ((EditText)findViewById(R.id.CurrentWeight)).getText().toString();
TargetWeight = ((EditText)findViewById(R.id.TargetWeight)).getText().toString();


if (Name.isEmpty()){
Toast.makeText(getApplicationContext(), "Name cannot be empty!", Toast.LENGTH_LONG).show();
return;
}
if (Gender.isEmpty()){
Toast.makeText(getApplicationContext(), "Gender cannot be empty!", Toast.LENGTH_LONG).show();
return;
}
if (DOB.isEmpty()){
Toast.makeText(getApplicationContext(), "DOB cannot be empty!", Toast.LENGTH_LONG).show();
return;
}
if (Height.isEmpty()){
Toast.makeText(getApplicationContext(), "Height cannot be empty!", Toast.LENGTH_LONG).show();
return;
}
if (CurrentWeight.isEmpty()){
Toast.makeText(getApplicationContext(), "Current weight cannot be empty!", Toast.LENGTH_LONG).show();
return;
}
Float CurrentWeightNum = Float.parseFloat(CurrentWeight);
if (CurrentWeightNum < 43.0){
Toast.makeText(getApplicationContext(), "Your current weight is not supported by this application", Toast.LENGTH_LONG).show();
return;
}

if (TargetWeight.isEmpty()){
Toast.makeText(getApplicationContext(), "Target weight cannot be empty!", Toast.LENGTH_LONG).show();
return;
}
Float TargetWeightNum = Float.parseFloat(TargetWeight);
if (TargetWeightNum > 93.0){
Toast.makeText(getApplicationContext(), "Your target weight is not supported by this application", Toast.LENGTH_LONG).show();
return;
}
if (ActivityLevel.isEmpty()){
Toast.makeText(getApplicationContext(), "Activity level cannot be empty!", Toast.LENGTH_LONG).show();
return;
}
if (WeightDifference.isEmpty()){
Toast.makeText(getApplicationContext(), "Weight difference cannot be empty!", Toast.LENGTH_LONG).show();
return;
}

if (CurrentWeightNum.equals(TargetWeightNum)){
Toast.makeText(getApplicationContext(), "Current and target weight can not be the same!", Toast.LENGTH_LONG).show();
return;
}

dbhelper.addUserData(Name, Gender, DOB, Height, CurrentWeightNum, TargetWeightNum, ActivityLevel, WeightDifference);

Toast.makeText(getApplicationContext(), "You have been saved!", Toast.LENGTH_LONG).show();

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);


}


}

Sorry if formatting is not correct, the application currently does not open, however when I remove the dbhelper stuff in the set up screen class the application runs but crashes when I try to press enter which calls the adding data method. I have investigated the issue but can not see where I am going wrong.
Thanks

Answer

Try to remove:

public void onCreate(SQLiteDatabase db) {
    db.execSQL(SQL_CREATE_USER);
    db.close(); // remove this line
}

And also this:

onCreate(this.getWritableDatabase());

From onUpgrade() method, add:

db.execSQL("DROP TABLE IF EXISTS " + YOUR_TABLE_NAME);
onCreate(db);