-1

Tab をクリックするたびに電話から連絡先リストを読み込んでいます。最初のタッチでは問題なく動作しますが、2 回目からメモリの問題が発生します。この問題を防ぐにはどうすればよいですか?

これがコードです。

String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";
try {
        contactsCursor= getContentResolver()
        .query(uri, null, null, null,  sortOrder);

        //Log.i("The first one", "" + contactsCursor.getCount());
        if(contactsCursor.getCount()>0){
            if(contactsCursor.moveToFirst()){
                while (contactsCursor.moveToNext()) {

                    String hasPhoneNumber = contactsCursor.getString(contactsCursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    int contTypeInt=0;
                    String contactType="";
                    ArrayList<String> phoneNumberList= new ArrayList<String>();
                    if (Integer.parseInt(hasPhoneNumber) > 0) {

                        String id = contactsCursor.getString(contactsCursor
                                .getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                        Cursor phones = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
                        String phoneNumber = null;
                        if(phones!=null && phones.getCount()>0){
                            while (phones.moveToNext()) {
                                int type= phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                                if (type == Phone.TYPE_MOBILE){
                                    phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    phoneNumberList.add(phoneNumber);
                                }
                            }
                            contTypeInt++;
                            phones.close();
                            phones = null;
                        }
                    }

                    String id = contactsCursor.getString(contactsCursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                    String name = contactsCursor.getString(contactsCursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                    Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + id, null, null);
                    ArrayList<String> emailAddressList= new ArrayList<String>();
                    if(emails!=null && emails.getCount()>0){
                        while (emails.moveToNext()) 
                           {
                               // This would allow you get several email addresses
                               String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                               if(strEmails.equals("")){
                                   strEmails= emailAddress;
                               }else{
                                   strEmails= strEmails+","+emailAddress;
                               }
                               Log.v(name+"==>", emailAddress);
                               if ((!emailAddress.equalsIgnoreCase(""))&&(emailAddress.contains("@"))) 
                               {   
                                Log.d("email", emailAddress);
                                   emailAddressList.add(emailAddress);
                               }
                           }
                        emails.close();
                        emails = null;
                    }

                    if(emailAddressList.size()>0){
                        //primaryEmailList.add(emailAddressList.get(0));
                        contTypeInt++;
                    }
                    if(contTypeInt==0){
                        contactType="";
                    }else if(contTypeInt==2){
                        contactType= "both";
                    }else if(contTypeInt==1){
                        if(emailAddressList.size()>0){
                            contactType="email";
                        }else{
                            contactType="phone";
                        }
                    }
                    Log.d(name+" conatctType", contactType);
                    if(name==null){
                        name="";
                    }

                    String []tempArr= name.split(" ");
                    for(int i=0;i<tempArr.length;i++){
                        if(tempArr[i].length()>1){
                            tempArr[i]= tempArr[i].substring(0, 1).toUpperCase() + tempArr[i].substring(1);
                        }
                    }
                    String nameTmp="";
                    for(int i=0;i<tempArr.length;i++){
                        if(nameTmp.equals("")){
                            nameTmp= tempArr[i];
                        }else{
                            nameTmp= nameTmp+" "+tempArr[i];
                        }

                    }
                    Constants.contactsList.add(new ContactsData(id, nameTmp, contactType, phoneNumberList, emailAddressList));
                    phoneNumberList = null;
                    emailAddressList = null;
                    tempArr = null;
                }
            }
        }
        contactsCursor.close();
        contactsCursor = null;
        }
        catch (Exception e) {

        }

私を助けてくれる人はいますか?前もって感謝します

4

2 に答える 2

0

あなたのエラーは、連絡先をロードするこの Fragment が既に作成されているかどうかを確認していないことが原因である可能性があります。onCreate()これを行うには、フラグメント内のメソッドに渡される Bundle が null かどうかを確認する必要があります。フラグメントが初めて作成される場合にのみ、連絡先をロードする必要があります。例:

public void onCreate (Bundle savedInstanceState) {

    if(savedInstanceState == null) {
        // load the contacts
    }
}

レイアウトが縦向きから横向きに、またはその逆に変更されるたびにフラグメントが再作成されるため、これはフラグメントで行うことが重要であることに注意してください。

于 2013-09-04T17:33:06.103 に答える