我在android studio中使用严格模式,但我无法找到导致错误的代码。错误是由于未关闭的对象引起的,但我检查了所有的活动,没有这样的对象有被打开了。
错误记录
02-10 17:25:21.858 29937-29945/com.user E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:180)
at android.database.CursorWindow.<init>(CursorWindow.java:111)
at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:140)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:134)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:219)
at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:264)
at com.google.android.gms.internal.zzatg.zza(Unknown Source)
at com.google.android.gms.internal.zzatg.zza(Unknown Source)
at com.google.android.gms.internal.zzatw.zzc(Unknown Source)
at com.google.android.gms.internal.zzatu.zzb(Unknown Source)
at com.google.android.gms.internal.zzatu.zza(Unknown Source)
at com.google.android.gms.internal.zzatu$4.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.gms.internal.zzato$zzd.run(Unknown Source)
02-10 17:25:21.859 29937-29945/com.user E/StrictMode: Finalizing a Cursor that has not been deactivated or closed. database = /data/user/0/com.user/databases/google_app_measurement_local.db, table = null, query = select count(1) from messages
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:98)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:50)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.google.android.gms.internal.zzatg.zza(Unknown Source)
at com.google.android.gms.internal.zzatg.zza(Unknown Source)
at com.google.android.gms.internal.zzatw.zzc(Unknown Source)
at com.google.android.gms.internal.zzatu.zzb(Unknown Source)
at com.google.android.gms.internal.zzatu.zza(Unknown Source)
at com.google.android.gms.internal.zzatu$4.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.gms.internal.zzato$zzd.run(Unknown Source)
我的BaseActivity类
public class BaseActivity extends AppCompatActivity {
public User_Registration user;
private Realm realmDb;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("called_base", "base");
realmDb = Realm.getDefaultInstance();
user = realmDb.where(User_Registration.class).findFirst();
if (user == null)
logout();
}
protected void logout() {
realmDb.executeTransaction(realm -> {
RealmResults<User_Registration> all = realm.where(User_Registration.class).findAll();
Log.i("loggingout", "size" + all.size());
all.deleteAllFromRealm();
redirect();
});
Log.i("loggingout", "called");
}
private void redirect() {
final Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
finishAffinity();
startActivity(intent);
}
private void closeDb() {
if (realmDb != null && !realmDb.isClosed())
realmDb.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("realm", "onDestroyActivityBase");
closeDb();
}
}
在我的活动中,我获取数据的方式
private void fetchData() {
userDataRepository dataRepository = new userDataRepository();
Observable<ArrayList<user>> observable = dataRepository.getuser_List();
observable.subscribe(users -> {
Log.i("data", users.size() + "");
items.addAll(users);
userAdapter.notifyItemRangeInserted(0, users.size());
});
}
UserDataRepository类
public class UserDataRepository implements UserDataStore, Lifecycle {
private UserDiskData diskData;
public UserDataRepository() {
this.diskData = new UserDiskData();
}
@Override
public Observable<ArrayList<User>> getUser_List() {
if (diskData.Users_Size() > 0) {
return diskData.getUser_List();
} else {
final UserCloudData cloudData = new UserCloudData();
Observable<ArrayList<User>> observable = cloudData.getUser_List();
diskData.addAllUser(observable);
return observable;
}
}
@Override
public Single<User> getUser(int user_id) {
if (diskData.isExists(user_id)) {
return diskData.getUser(user_id);
} else {
final UserCloudData cloudData = new UserCloudData();
Single<User> UserSingle = cloudData.getUser(user_id);
diskData.addUser(UserSingle);
return UserSingle;
}
}
@Override
public void onDestroyCall() {
diskData.onDestroyCall();
}
}
UserDiskData.class
class UserDiskData implements UserDataStore, Lifecycle {
int Users_Size() {
final Realm realm = Realm.getDefaultInstance();
final RealmResults<User> results = realm.where(User.class).findAll();
Log.i("disk", "pulledData" + results.size());
realm.close();
return results.size();
}
void addAllUser(Observable<ArrayList<User>> Users) {
Log.i("disk", "addAll");
Users.subscribe(Users1 -> {
final Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(realmDb -> realmDb.copyToRealmOrUpdate(Users1));
realm.close();
});
}
void addUser(Single<User> User) {
Log.i("disk", "addSingle");
User.subscribe(Users1 -> {
final Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(realmDb -> realmDb.copyToRealmOrUpdate(Users1));
realm.close();
});
}
@Override
public Observable<ArrayList<User>> getUser_List() {
Log.i("disk", "called");
final Realm realm = Realm.getDefaultInstance();
final RealmResults<User> results = realm.where(User.class).findAll();
ArrayList<User> list = new ArrayList<>();
for (int i = 0; i < results.size(); i++)
list.add(results.get(i));
realm.close();
Log.i("realmDisk", realm.isClosed() + "");
return Observable.just(list);
}
boolean isExists(int id) {
Log.i("disk", "isExists");
final Realm realm = Realm.getDefaultInstance();
final User User = realm.where(User.class).equalTo("id", id).findFirst();
realm.close();
//return User != null;
return false;
}
@Override
public Single<User> getUser(int user_id) {
Log.i("disk", "getSingle");
final Realm realm = Realm.getDefaultInstance();
final User User = realm.where(User.class).equalTo("id", user_id).findFirst();
realm.close();
return Single.just(User);
}
@Override
public void onDestroyCall() {
}
}
这些是使用领域的唯一类。