I'm new to Android. I can now register the value of EditText in the database while looking at the text or web page, but when I try to enter it continuously, it fails. ** You can only register data once each time you open the app! ** ** I thought this was fatal as an app and investigated what to do. I think there are two factors. ➀ try-catch-finally statement not written ➁ Wrong location to open database I decided to write this article as a reference for those who are suffering from the same situation.
As you can see from the code after this, my code doesn't have the necessary try-catch, it's very unstable because it didn't have a catch statement in the first place. I understand the importance of try-catch, but it's difficult. .. I thought I still needed to study. I think it is better to use "** try-with-resources **" described later rather than thinking about complicated try-catch.
Click here for the full code (jump to another article I wrote) If you register only once, it will not fall. If you try to register twice, you will get the exception "java.lang.illegalStateException attempt to re-open an already-closed object". That's because the falling code opens the database with the onCreate method. The onCreate method is the first method to be executed when the app is started. I open the database here, do some processing and then release the database. Even if I try to operate it again, the app crashes because it was closed by onCreate.
MainActivity.java
public void insertData(SQLiteDatabase db, String maxBP, String minBP, String pulse){
ContentValues values = new ContentValues();
try {
values.put("_maxBP", maxBP);
values.put("_minBP", minBP);
values.put("_pulse", pulse);
db.insert("_BPtable", null, values);
} finally {
db.close();
}
}
try-with-resources I think the advantage of try-with-resources is that you don't have to write complicated try-catch statements. ~~ I don't understand try-catch, which is a big help ~~ However, I think that there are many programs that only try-catch-finally are implemented, so I will do my best to study. .. Click here for try-with-resources syntax
python
try (Declaration of variables that need to be cleaned up by close) {
Original processing
} catch (Exception class variable name) {
What to do if an exception occurs
}
Here is a rewrite of the previous code using try-with-resources In addition, I reviewed the code to open the database with methods. ➁ It is a solution.
MainActivity.java
public void insertData(String maxBP, String minBP, String pulse){
ContentValues values = new ContentValues();
DatabaseHelper helper = new DatabaseHelper(MainActivity.this);
try (SQLiteDatabase data = helper.getWritableDatabase()){
values.put("_maxBP", maxBP);
values.put("_minBP", minBP);
values.put("_pulse", pulse);
data.insert("_BPtable", null, values);
} catch (Exception e){
e.printStackTrace();
}
}
This will automatically release the database after the try is finished. We have created a program that will not drop continuous registration!
WINGS Project by Shinzo Saito / Supervised by Yoshihiro Yamada "Textbook for Android Application Development" Written by Kiyotaka Nakayama / Written by Daigo Kunimoto / Supervised by Flarelink Co., Ltd. "Introduction to Java for a refreshing 3rd edition"
Recommended Posts