手机类型和电子邮件类型不工作?手机类型、电子邮件、类型、工作

2023-09-04 05:10:28 作者:也许幸福它很忙

在我的项目中的电话类型,电子邮件类型都有一些疑难问题。现在用2.1的平台,有增加的手机类型和邮件类型,使用这些手机类型和电子邮件的类型,当我尝试后,才在我的项目没有问题运行该项目时,它打开一个消息框,并显示应用停止,强制关闭。我的XML文件中有一个按钮和ListView控件。我用这code。

code

 公共类GetAllDatas延伸活动{

ListView的LVITEM;
私人按钮btnAdd;
字符串P型,协议类型;
INT contactPhoneType;

字符串显示名=,EMAILADDRESS =,phoneNumber的=;
ArrayList的<字符串> contactlist =新的ArrayList<字符串>();
ArrayAdapter<字符串> itemAdapter;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    LVITEM =(ListView控件)this.findViewById(R.id.lvitems);
    btnAdd =(按钮)this.findViewById(R.id.btnAddItem);

    itemAdapter =新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,contactlist);
    lvItem.setAdapter(itemAdapter);

    btnAdd.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            readContacts();
        }
    });

}

私人无效readContacts()
{
    ContentResolver的CR = getContentResolver();
    光标光标= cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);


    而(cursor.moveToNext())
    {
        显示名=;
        ArrayList的<字符串> phoneNumber的=新的ArrayList<字符串>();
        ArrayList的<字符串> EMAILADDRESS =新的ArrayList<字符串>();

        显示名= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        字符串ID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

        /*                   电子邮件                   */

        光标电子邮件= cr.query(Email.CONTENT_URI,空,Email.CONTACT_ID +=+ ID,NULL,NULL);
        而(emails.moveToNext())
        {
            emailAddress.add(emails.getString(emails.getColumnIndex(Email.DATA)));

        }

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

        开关(contactPhoneType){
        案例ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
        协议类型=家;
        打破;

        }


        emails.close();


      / *电话号码和类型* /

        if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
        {
            光标pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=?,新的String [] {ID},NULL);
           而(pCur.moveToNext())
            {
                 phoneNumber.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
           }

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


           开关(contactPhoneType){
           案例ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
           P型=家;
           打破;

           }


            pCur.close();
        }

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

//添加数组列表数据contactlist ..

如果(phoneNumberCount> emailCount){

    的for(int i = 0; I< phoneNumberCount;我++)
      {
        如果(emailCount I标记)
          {
           contactlist.add(显示名+,+ PTYPE +,+ phoneNumber.get(I)+,+协议类型+,+ emailAddress.get(ⅰ)+\ñ);
           }
        其他
        {
          contactlist.add(显示名+,+ PTYPE +,+ phoneNumber.get(ⅰ)+,+,+\ñ);
        }
       }
    }
    其他
       {
        的for(int i = 0; I< emailCount;我++)
         {
        如果(phoneNumberCount I标记)
          {
            contactlist.add(显示名+,+ PTYPE +,+ phoneNumber.get(I)+,+协议类型+,+ emailAddress.get(ⅰ)+\ñ);
          }
        其他
        {
          contactlist.add(显示名+,+,+,+协议类型+,+ emailAddress.get(ⅰ)+\ñ);
        }
      }
    }
    }
    Collections.sort(contactlist);
    itemAdapter.notifyDataSetChanged();
    cursor.close();
}
 

}

的logcat

  06-26 13:31:16.213:E / AndroidRuntime(449):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
06-26 13:31:16.224:E / AndroidRuntime(449):android.database.CursorIndexOutOfBoundsException:指数1的要求,为1的大小
06-26 13:31:16.224:E / AndroidRuntime(449):在android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
06-26 13:31:16.224:E / AndroidRuntime(449):在android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
06-26 13:31:16.224:E / AndroidRuntime(449):在android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
06-26 13:31:16.224:E / AndroidRuntime(449):在android.database.CursorWrapper.getInt(CursorWrapper.java:123)
06-26 13:31:16.224:E / AndroidRuntime(449):在com.abhi.GetAllDatas.readContacts(GetAllDatas.java:71)
06-26 13:31:16.224:E / AndroidRuntime(449):在com.abhi.GetAllDatas.access $ 0(GetAllDatas.java:47)
06-26 13:31:16.224:E / AndroidRuntime(449):在com.abhi.GetAllDatas $ 1.onClick(GetAllDatas.java:41)
 
我苹果手机怎么用电子邮件发信息发不出去怎么弄

解决方案

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

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

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

             开关(contactPhoneType){
                案例ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                     P型=家;
                     打破;
           }
       }
 

做同样的修正的emailType为好。

The phone type and Email type in my project has some problem.I am using 2.1 platform, there was no problem in my project before adding the Phone type and the Email type, after using these Phone type and Email type when I try to run the project it open a message box and shows application stopped and force close. My xml file has a Button and the ListView. i am using this code.

code

 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(); 
} 

}

Logcat

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)

解决方案

I got the error in your code. You are checking the phoneType as well as the emailtype after moving the cursor beyond the last record (pCur.moveToNext() after the while loop goes beyond the last record). You need to check it within the while loop and not after if. Change the following:

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;
           }                  
       }

do the same correction for the emailType as well.