0

我项目中的电话类型和电子邮件类型有问题。我使用的是2.1平台,我的项目在添加电话类型和电子邮件类型之前没有问题,使用这些电话类型和电子邮件类型后,我尝试运行项目它打开一个消息框并显示应用程序已停止并强制关闭。我的 xml 文件有一个 Button 和 ListView。我正在使用此代码。

代码

 public class GetAllDatas extends Activity {

ListView lvItem;
private Button btnAdd;
String Ptype, Etype;
int contactPhoneType;

String displayName="", emailAddress="", phoneNumber="";
ArrayList<String> contactlist=new ArrayList<String>(); 
ArrayAdapter<String> itemAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lvItem = (ListView)this.findViewById(R.id.lvitems); 
    btnAdd = (Button)this.findViewById(R.id.btnAddItem);

    itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
    lvItem.setAdapter(itemAdapter);

    btnAdd.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) {
            readContacts();
        }
    });

} 

private void readContacts()
{
    ContentResolver cr =getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);


    while (cursor.moveToNext()) 
    {
        displayName  = "" ;
        ArrayList<String> phoneNumber  = new ArrayList<String>();
        ArrayList<String> emailAddress  = new ArrayList<String>();

        displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

        /*                   Email                   */ 

        Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
        while (emails.moveToNext()) 
        { 
            emailAddress.add(emails.getString(emails.getColumnIndex(Email.DATA)));

        } 

         contactPhoneType = emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

        switch(contactPhoneType){
        case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
        Etype = "Home";
        break;

        }


        emails.close(); 


      /*            Phone Number and Type   */ 

        if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
        {
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
           while (pCur.moveToNext()) 
            {
                 phoneNumber.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));                               
           }

            contactPhoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));


           switch(contactPhoneType){
           case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
           Ptype = "Home";
           break;

           }


            pCur.close();  
        }   

int phoneNumberCount = phoneNumber.size();
int emailCount = emailAddress.size();

// Add arraylist data to contactlist..

if(phoneNumberCount > emailCount) {

    for (int i=0; i<phoneNumberCount; i++)
      {
        if(emailCount>i)
          {
           contactlist.add(displayName +  " , " +  Ptype + " , " + phoneNumber.get(i) + " , " + Etype + " , " + emailAddress.get(i) + "\n");    
           }
        else
        {
          contactlist.add(displayName + " , " +  Ptype + " , " + phoneNumber.get(i) + " , " + " , " + "\n");
        } 
       }
    }
    else  
       {
        for (int i=0; i<emailCount; i++) 
         {
        if(phoneNumberCount>i)
          {
            contactlist.add(displayName + " , " + Ptype + " , " + phoneNumber.get(i) + " , " +  Etype + " , " + emailAddress.get(i) + "\n"); 
          }
        else
        { 
          contactlist.add(displayName + " , " + " , " + " , " + Etype + " , " + emailAddress.get(i) +  "\n");
        }
      }
    }      
    }
    Collections.sort(contactlist);
    itemAdapter.notifyDataSetChanged();
    cursor.close(); 
} 

}

日志猫

06-26 13:31:16.213: E/AndroidRuntime(449): Uncaught handler: thread main exiting due to   uncaught exception
06-26 13:31:16.224: E/AndroidRuntime(449): android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
06-26 13:31:16.224: E/AndroidRuntime(449):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
06-26 13:31:16.224: E/AndroidRuntime(449):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
06-26 13:31:16.224: E/AndroidRuntime(449):  at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
06-26 13:31:16.224: E/AndroidRuntime(449):  at android.database.CursorWrapper.getInt(CursorWrapper.java:123)
06-26 13:31:16.224: E/AndroidRuntime(449):  at com.abhi.GetAllDatas.readContacts(GetAllDatas.java:71)
06-26 13:31:16.224: E/AndroidRuntime(449):  at com.abhi.GetAllDatas.access$0(GetAllDatas.java:47)
06-26 13:31:16.224: E/AndroidRuntime(449):  at com.abhi.GetAllDatas$1.onClick(GetAllDatas.java:41)
4

4 回答 4

0

正如 Dheeresh 所说,您必须更改该条件

于 2012-06-27T06:14:21.790 回答
0

看起来问题出在索引上

android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1

gor 获取 Phone.TYPE 可以按照此http://www.vtgroup.com/#ContactsContract尝试以下代码

    //  Get all phone numbers.
    //
    Cursor phones = cr.query(Phone.CONTENT_URI, null,
        Phone.CONTACT_ID + " = " + contactId, null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
        switch (type) {
            case Phone.TYPE_HOME:
                // do something with the Home number here...
                break;
            case Phone.TYPE_MOBILE:
                // do something with the Mobile number here...
                break;
            case Phone.TYPE_WORK:
                // do something with the Work number here...
                break;
            }
    }
    phones.close();
于 2012-06-26T08:24:43.060 回答
0

可能是这一行:

while (cursor.moveToNext()) 

对我来说,这看起来就像你在读取第一个光标位置之前就移动了光标,并且你得到了 CursorIndexOOB 异常。

请尝试使用 do-while 循环,看看是否有帮助。

此外,请检查您的应用程序是否有权读取联系人数据。

uses-permission android:name="android.permission.READ_CONTACTS"
于 2012-06-26T08:17:50.547 回答
0

我在你的代码中得到了错误。while在将光标移到最后一条记录之后(循环超出最后一条记录后的 pCur.moveToNext()),您正在检查 phoneType 和 emailtype 。您需要在while循环内而不是在 if 之后检查它。更改以下内容:

while (pCur.moveToNext()) 
        {
             phoneNumber.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 

             int contactPhoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

             switch(contactPhoneType){
                case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
                     Ptype = "Home";
                     break;
           }                  
       }

对 emailType 也进行相同的更正。

于 2012-06-26T08:17:58.077 回答