这是我打开监护人的视图时得到的错误我打开了数据库但是当我从活动中出去并尝试添加新联系人这个错误出现在我身上时我尝试关闭数据库意味着但它根本不起作用。
03-27 12:09:05.705: E/Database(14728): close() was never explicitly called on database '/data/data/com.project.smartcare/databases/Guardiandb'
03-27 12:09:05.705: E/Database(14728): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-27 12:09:05.705: E/Database(14728): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1855)
03-27 12:09:05.705: E/Database(14728): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:824)
03-27 12:09:05.705: E/Database(14728): at com.project.smartcare.Database$DbHelper.openDatabase(Database.java:98)
03-27 12:09:05.705: E/Database(14728): at com.project.smartcare.SqlView.onStart(SqlView.java:41)
03-27 12:09:05.705: E/Database(14728): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
03-27 12:09:05.705: E/Database(14728): at android.app.Activity.performStart(Activity.java:3791)
03-27 12:09:05.705: E/Database(14728): at android.app.Activity.performRestart(Activity.java:3821)
03-27 12:09:05.705: E/Database(14728): at android.app.Activity.performResume(Activity.java:3826)
03-27 12:09:05.705: E/Database(14728): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2131)
03-27 12:09:05.705: E/Database(14728): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2156)
03-27 12:09:05.705: E/Database(14728): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
03-27 12:09:05.705: E/Database(14728): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 12:09:05.705: E/Database(14728): at android.os.Looper.loop(Looper.java:130)
03-27 12:09:05.705: E/Database(14728): at android.app.ActivityThread.main(ActivityThread.java:3701)
03-27 12:09:05.705: E/Database(14728): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 12:09:05.705: E/Database(14728): at java.lang.reflect.Method.invoke(Method.java:507)
03-27 12:09:05.705: E/Database(14728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
03-27 12:09:05.705: E/Database(14728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
03-27 12:09:05.705: E/Database(14728): at dalvik.system.NativeStart.main(Native Method)
03-27 12:09:05.715: E/Database(14728): close() was never explicitly called on database '/data/data/com.project.smartcare/databases/Guardiandb'
03-27 12:09:05.715: E/Database(14728): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-27 12:09:05.715: E/Database(14728): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1855)
03-27 12:09:05.715: E/Database(14728): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:824)
03-27 12:09:05.715: E/Database(14728): at com.project.smartcare.Database$DbHelper.openDatabase(Database.java:98)
03-27 12:09:05.715: E/Database(14728): at com.project.smartcare.SqlView.onRestart(SqlView.java:67)
03-27 12:09:05.715: E/Database(14728): at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1139)
03-27 12:09:05.715: E/Database(14728): at android.app.Activity.performRestart(Activity.java:3815)
03-27 12:09:05.715: E/Database(14728): at android.app.Activity.performResume(Activity.java:3826)
03-27 12:09:05.715: E/Database(14728): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2131)
03-27 12:09:05.715: E/Database(14728): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2156)
03-27 12:09:05.715: E/Database(14728): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
03-27 12:09:05.715: E/Database(14728): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 12:09:05.715: E/Database(14728): at android.os.Looper.loop(Looper.java:130)
03-27 12:09:05.715: E/Database(14728): at android.app.ActivityThread.main(ActivityThread.java:3701)
03-27 12:09:05.715: E/Database(14728): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 12:09:05.715: E/Database(14728): at java.lang.reflect.Method.invoke(Method.java:507)
03-27 12:09:05.715: E/Database(14728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
03-27 12:09:05.715: E/Database(14728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
03-27 12:09:05.715: E/Database(14728): at dalvik.system.NativeStart.main(Native Method)
03-27 12:09:05.735: E/Database(14728): close() was never explicitly called on database '/data/data/com.project.smartcare/databases/Guardiandb'
03-27 12:09:05.735: E/Database(14728): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-27 12:09:05.735: E/Database(14728): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1855)
03-27 12:09:05.735: E/Database(14728): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:824)
03-27 12:09:05.735: E/Database(14728): at com.project.smartcare.Database$DbHelper.openDatabase(Database.java:98)
03-27 12:09:05.735: E/Database(14728): at com.project.smartcare.SqlView.onResume(SqlView.java:84)
03-27 12:09:05.735: E/Database(14728): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
03-27 12:09:05.735: E/Database(14728): at android.app.Activity.performResume(Activity.java:3832)
03-27 12:09:05.735: E/Database(14728): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2131)
03-27 12:09:05.735: E/Database(14728): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2156)
03-27 12:09:05.735: E/Database(14728): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
03-27 12:09:05.735: E/Database(14728): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 12:09:05.735: E/Database(14728): at android.os.Looper.loop(Looper.java:130)
03-27 12:09:05.735: E/Database(14728): at android.app.ActivityThread.main(ActivityThread.java:3701)
03-27 12:09:05.735: E/Database(14728): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 12:09:05.735: E/Database(14728): at java.lang.reflect.Method.invoke(Method.java:507)
03-27 12:09:05.735: E/Database(14728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
03-27 12:09:05.735: E/Database(14728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
03-27 12:09:05.735: E/Database(14728): at dalvik.system.NativeStart.main(Native Method)
03-27 12:09:05.745: E/Database(14728): close() was never explicitly called on database '/data/data/com.project.smartcare/databases/Guardiandb'
03-27 12:09:05.745: E/Database(14728): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-27 12:09:05.745: E/Database(14728): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1855)
03-27 12:09:05.745: E/Database(14728): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:824)
03-27 12:09:05.745: E/Database(14728): at com.project.smartcare.Database$DbHelper.openDatabase(Database.java:98)
03-27 12:09:05.745: E/Database(14728): at com.project.smartcare.SqlView.onStart(SqlView.java:41)
03-27 12:09:05.745: E/Database(14728): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
03-27 12:09:05.745: E/Database(14728): at android.app.Activity.performStart(Activity.java:3791)
03-27 12:09:05.745: E/Database(14728): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1632)
03-27 12:09:05.745: E/Database(14728): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
03-27 12:09:05.745: E/Database(14728): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
03-27 12:09:05.745: E/Database(14728): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
03-27 12:09:05.745: E/Database(14728): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 12:09:05.745: E/Database(14728): at android.os.Looper.loop(Looper.java:130)
03-27 12:09:05.745: E/Database(14728): at android.app.ActivityThread.main(ActivityThread.java:3701)
03-27 12:09:05.745: E/Database(14728): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 12:09:05.745: E/Database(14728): at java.lang.reflect.Method.invoke(Method.java:507)
03-27 12:09:05.745: E/Database(14728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
03-27 12:09:05.745: E/Database(14728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
03-27 12:09:05.745: E/Database(14728): at dalvik.system.NativeStart.main(Native Method)
This is the acticity where the logcat point outs to.`enter code here`
public class SqlView extends Activity {
public DbHelper dbGuardians = null;
private Cursor ourCursor = null;
private GuardiansAdapter adapter = null;
ListView myListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
myListView = (ListView) findViewById(R.id.myList);
System.out.println("3adda onCreate");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
dbGuardians = new DbHelper(this);
dbGuardians.createDatabase();
dbGuardians.openDatabase();
ourCursor = dbGuardians.getCursor();
startManagingCursor(ourCursor);
adapter = new GuardiansAdapter(ourCursor);
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(OnListClick);
System.out.println("3adda on Start");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
ourCursor.close();
dbGuardians.close();
dbGuardians.closeDatabase();
System.out.println("3adda on Stop");
super.onStop();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
dbGuardians.openDatabase();
ourCursor = dbGuardians.getCursor();
startManagingCursor(ourCursor);
System.out.println("3adda on Restart");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
ourCursor.close();
dbGuardians.close();
dbGuardians.closeDatabase();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
dbGuardians.openDatabase();
ourCursor = dbGuardians.getCursor();
startManagingCursor(ourCursor);
System.out.println("3adda on Resume");
}
private AdapterView.OnItemClickListener OnListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
try {
String[] informationReceived = new String[5];
dbGuardians.openDatabase();
informationReceived = dbGuardians.searchEntry(String
.valueOf(id));
String firstName = informationReceived[0];
String lastName = informationReceived[1];
String number = informationReceived[2];
String email = informationReceived[3];
String status = informationReceived[4];
Bundle bundle = new Bundle();
bundle.putString("First Name", firstName);
bundle.putString("Last Name", lastName);
bundle.putString("Phone", number);
bundle.putString("Email", email);
if (status.equalsIgnoreCase("0")) {
bundle.putString("Status", "Passive");
} else {
bundle.putString("Status", "Active");
}
Intent i = new Intent(SqlView.this, GuardianView.class);
i.putExtras(bundle);
startActivity(i);
} catch (Exception e) {
System.out.println(e.toString() + "SqlView Exception ;)");
}finally{
ourCursor.close();
dbGuardians.closeDatabase();
dbGuardians.close();
}
}
};
class GuardiansAdapter extends CursorAdapter {
public GuardiansAdapter(Cursor c) {
super(SqlView.this, c);
}
public void bindView(View row, Context ctxt, Cursor c) {
GuardiansHolder holder = (GuardiansHolder) row.getTag();
holder.populateFrom(c, dbGuardians);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
GuardiansHolder holder = new GuardiansHolder(row);
row.setTag(holder);
return (row);
}
}
static class GuardiansHolder {
private TextView name = null;
GuardiansHolder(View row) {
name = (TextView) row.findViewById(R.id.myText);
}
void populateFrom(Cursor cursor, DbHelper guardians) {
name.setText(guardians.getName(cursor));
}
}
}
and that's my Database Activity where the database is created.
public class Database {
public static final String Key_RowId = "_id";
public static final String guardianFirstName = "FirstName";
public static final String guardianLastName = "LastName";
public static final String status = "GuardianStatus";
public static final String phone_number = "PhoneNumber";
public static final String guardian_email = "GuardianEmail";
private static final String databaseName = "Guardiandb";
public static String databaseTableOne = "Guardians";
private static final int databaseVersion = 1;
private static final String DATABASE_PATH = "/data/data/com.project.smartcare/databases/";
public DbHelper ourHelper;
private final Context ourContext;
public SQLiteDatabase ourDatabase;
public static class DbHelper extends SQLiteOpenHelper {
private final Context ourContextOne;
public SQLiteDatabase ourDatabaseOne;
public DbHelper(Context context) {
super(context, databaseName, null, databaseVersion);
// TODO Auto-generated constructor stub
this.ourContextOne = context;
}
public void createDatabase() {
boolean dbExist = DBExists();
if (!dbExist) {
this.getWritableDatabase();
copyDBFromResource();
}
}
private boolean DBExists() {
SQLiteDatabase db = null;
try {
String databasePath = DATABASE_PATH + databaseName;
db = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SQL helper", "Database not found");
}
if (db != null) {
db.close();
}
return db != null ? true : false;
}
private void copyDBFromResource() {
InputStream inputStream = null;
OutputStream outputStream = null;
String dbFilePath = DATABASE_PATH + databaseName;
try {
inputStream = ourContextOne.getAssets().open(databaseName);
outputStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e) {
throw new Error("Problem copying form database");
}
}
public void openDatabase() throws SQLException {
String myPath = DATABASE_PATH + databaseName;
ourDatabaseOne = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public Cursor getCursor() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(databaseTableOne);
String[] asColumnsToReturn = new String[] { Key_RowId,
guardianFirstName, guardianLastName, phone_number,
guardian_email, status };
Cursor mCursor = queryBuilder.query(ourDatabaseOne,
asColumnsToReturn, null, null, null, null, null);
return mCursor;
}
public ArrayList<String> getPhoneNo(Cursor c) {
ArrayList<String> a;
a = new ArrayList<String>();
c = getCursor();
String phone = null;
while (c.moveToNext()) {
phone = c.getString(c.getColumnIndex(phone_number));
a.add(phone);
System.out.println(phone);
}
System.out.println("3adeeet mn el get phone num");
return a;
}
public synchronized void closeDatabase() {
if (ourDatabaseOne != null) {
ourDatabaseOne.close();
}
super.close();
}
public String getName(Cursor c) {
return (c.getString(1) + " " + c.getString(2));
}
public Cursor getCursorForNumber() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(databaseTableOne);
String[] asColumnsToReturn = new String[] { phone_number };
Cursor phoneCursor = queryBuilder.query(ourDatabaseOne,
asColumnsToReturn, null, null, null, null, null);
return phoneCursor;
}
public String getPhoneNumber(Cursor c) {
String s = c.getString(3);
System.out.println("this is working");
System.out.println(s);
return (c.getString(3));
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + databaseTableOne + " (" + Key_RowId
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ guardianFirstName + " TEXT NOT NULL, " + guardianLastName
+ " TEXT NOT NULL, " + phone_number + " TEXT NOT NULL, "
+ guardian_email + " TEXT, " + status + " BOOLEAN);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + databaseTableOne);
onCreate(db);
}
public String[] searchEntry(String reference) {
try {
String[] informationString = new String[5];
int referenceOne = Integer.parseInt(reference);
Cursor cursor = ourDatabaseOne.query(databaseTableOne,
new String[] { Key_RowId, guardianFirstName,
guardianLastName, phone_number, guardian_email,
status }, Key_RowId + " = " + referenceOne,
null, null, null, null);
if (cursor != null) {
try {
cursor.moveToFirst();
String firstName = cursor.getString(1);
String lastName = cursor.getString(2);
String number = cursor.getString(3);
String email = cursor.getString(4);
String status = cursor.getString(5);
informationString[0] = firstName;
informationString[1] = lastName;
informationString[2] = number;
informationString[3] = email;
informationString[4] = status;
return informationString;
} catch (Exception e) {
System.out.println(e.toString() + "Gowani");
}
}
} catch (Exception e) {
System.out.println(e.toString() + "Exception el barani");
}
return null;
}
}
public long createEntry(String firstName, String lastName, String number,
String email, String s) {
// TODO Auto-generated method stub
ContentValues contentValuesGuardian = new ContentValues();
contentValuesGuardian.put(guardianFirstName, firstName);
contentValuesGuardian.put(guardianLastName, lastName);
contentValuesGuardian.put(phone_number, number);
if (!email.equalsIgnoreCase("")) {
contentValuesGuardian.put(guardian_email, email);
}
if (s.equalsIgnoreCase("Active")) {
contentValuesGuardian.put(status, 1);
} else {
contentValuesGuardian.put(status, 0);
}
Long g = ourDatabase.insert(databaseTableOne, null,
contentValuesGuardian);
return g;
}
public void deleteEntry(String fName, String lName, String phone,
String email) {
// TODO Auto-generated method stub
ourDatabase.delete(databaseTableOne, phone_number + " = " + "\""
+ phone + "\"", null);
}
public void updateEntry(String fNameOld, String lNameOld, String fNameNew,
String lNameNew, String numberNew, String emailNew, String statusNew) {
// TODO Auto-generated method stub
try {
ContentValues contentValueGuardian = new ContentValues();
contentValueGuardian.put(guardianFirstName, fNameNew);
contentValueGuardian.put(guardianLastName, lNameNew);
contentValueGuardian.put(phone_number, numberNew);
if (!emailNew.equalsIgnoreCase("")) {
contentValueGuardian.put(guardian_email, emailNew);
}
if (statusNew.equalsIgnoreCase("Active")) {
contentValueGuardian.put(status, 1);
} else {
contentValueGuardian.put(status, 0);
}
Cursor cursor = ourDatabase.query(databaseTableOne, new String[] {
Key_RowId, guardianFirstName, guardianLastName },
guardianFirstName + " = " + "\"" + fNameOld + "\""
+ " AND " + guardianLastName + " = " + "\""
+ lNameOld + "\"", null, null, null, null);
int id = 0;
if (cursor != null) {
cursor.moveToFirst();
int guardid = cursor.getColumnIndex(Key_RowId);
String string = cursor.getString(guardid);
if (string != null) {
try {
id = Integer.parseInt(string);
} catch (Exception e) {
System.out.println("Feeh Error");
}
}
ourDatabase.update(databaseTableOne, contentValueGuardian,
Key_RowId + " = " + id, null);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void close() {
ourHelper.close();
}
public Database(Context context) {
ourContext = context;
}
public Database open() throws SQLiteException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
}
答案 0 :(得分:2)
当您打开数据库时,执行您的任务,之后需要关闭连接
这样的事情:
final TourDBAdapter d = new TourDBAdapter(getBaseContext());
final DatabaseHelper g = new DatabaseHelper(getBaseContext());
d.open();
// your work
d.close();