错误:WindowManager:android.view.WindowLeaked:Activity com.example.altu.calllocker.activity.Home已泄露窗口com.android.internal.policy.impl.PhoneWindow $ DecorView {22165159 VE ...最初添加的R ...... D 0,0-456,174}
我只是获取所有联系人并将其保存到领域以供离线但在加载联系人后它崩溃与上面提到错误....我已经完全实现了progreessDialog但是如何对话可能是崩溃的原因我没有得到它。
public class Home extends AppCompatActivity {
Switch sync;
SearchView searchView;
ContactAdapter contactAdapter;
Realm realm;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Realm.init(Home.this);
realm = Realm.getDefaultInstance();
sync = (Switch) findViewById(R.id.sync);
// progressDialog.show();
sync.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
sync.setChecked(isChecked);
new Async().execute();
}
}
});
}
private class Async extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
@Override
protected Void doInBackground(Void... params) {
getAllContact();
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(Home.this);
progressDialog.setMessage("Loading....");
progressDialog.show();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
}
private void getAllContact() {
RealmList<ContactsModel> contactlist = new RealmList<>();
ContactsModel contactVO;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactVO = new ContactsModel();
contactVO.setDisplay_name(name);
contactVO.set_id(id);
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactVO.setData1(phoneNumber);
}
Cursor emailCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emailCursor.close();
phoneCursor.close();
contactlist.add(contactVO);
}
}
HashSet<ContactsModel> hashSet = new HashSet<>();
hashSet.addAll(contactlist);
contactlist.clear();
contactlist.addAll(hashSet);
realm.beginTransaction();
realm.copyToRealmOrUpdate(contactlist);
realm.commitTransaction();
contactlist.clear();
RealmResults<ContactsModel> realmResults = realm.where(ContactsModel.class).findAll();
for (ContactsModel c : realmResults) {
Log.e("id", c.getData1());
Log.e("name", c.getDisplay_name());
contactlist.add(c);
}
}
}
}
答案 0 :(得分:0)
使getallcontacts方法重新实现realmlist并使用单独的方法将数据保存到领域
private RealmList<ContactsModel> getAllContact() {
RealmList<ContactsModel> contactlist = new RealmList<>();
// List<ContactsModel> contactsModelList = new ArrayList<>();
ContactsModel contactVO;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactVO = new ContactsModel();
contactVO.setDisplay_name(name);
contactVO.set_id(id);
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactVO.setData1(phoneNumber);
}
Cursor emailCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emailCursor.close();
phoneCursor.close();
// contactsModelList.add(contactVO);
contactlist.add(contactVO);
}
}
//saveToRealm(contactlist);
}
return contactlist;
}
public void saveToRealm(RealmList<ContactsModel> contactlist) {
/* ProgressDialog progressDialog = new ProgressDialog(Home.this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();*/
if (contactlist != null) {
HashSet<ContactsModel> hashSet = new HashSet<>();
hashSet.addAll(contactlist);
contactlist.clear();
contactlist.addAll(hashSet);
realm.beginTransaction();
realm.copyToRealmOrUpdate(contactlist);
realm.commitTransaction();
contactlist.clear();
RealmResults<ContactsModel> realmResults = realm.where(ContactsModel.class).findAll();
Log.e("size", realmResults.size() + "");
for (ContactsModel c : realmResults) {
Log.e("id", c.getData1());
Log.e("name", c.getDisplay_name());
contactlist.add(c);
}
// progressDialog.dismiss();
} else {
// do nothing
// progressDialog.dismiss();
}
}
并更改下面的setonchecklistener
sync.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
sync.setChecked(isChecked);
new Async().execute();
}
}
});
saveToRealm(getAllContact());}