我的应用程序崩溃当我想在我的应用程序中显示联系人姓名和号码时

时间:2014-05-12 05:26:32

标签: android numbers contacts android-contacts

我是一名新的Android开发人员,我希望在列表中显示联系人号码和联系人姓名,但当只有联系人姓名显示应用程序正常运行但是当我显示联系人号码时,应用程序崩溃我正在使用自定义适配器请可以任何一个帮助我

我的contactList活动正在关注

public class ContactList extends Activity {
    ListView lvContacts;
      List<String> listname;  
      List<String> listnumber;
    SimpleCursorAdapter adapter;
    ArrayList<PhoneList> arr;
    ArrayList<PhoneList> arrnumber;
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Cursor c1=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI , null, null, null, null);
            // Let activity manage the cursor
            //startManagingCursor(c1);


            listname = new ArrayList<String>();
            listnumber = new ArrayList<String>();
            c1.moveToFirst();
            while(c1.moveToNext())
            {
                Log.d("fghfh", ""+c1.getColumnIndex(Phone.NUMBER));
                String name=c1.getString(c1.getColumnIndex(Phone.DISPLAY_NAME));
                String  number=c1.getString(c1.getColumnIndex(Phone.NUMBER));
                //System.out.println(".................."+number); 


                 listname.add(name);
                listnumber.add(number);


               // Log.d("Contacts : ",name);//+":"+number);
            }
             Object[] obj=listname.toArray();
             Object[] objnumber=listnumber.toArray();
          //   arrnumber=new ArrayList<PhoneList>();
             arr=new ArrayList<PhoneList>();
             String[] da=new String[obj.length];
             String[] danumber=new String[objnumber.length];

             for(int i=0; i<obj.length; i++)
            {    
                 danumber[i]=(String)objnumber[i];
                 da[i]=(String)obj[i];
                 Toast.makeText(this, "Humayoon    zoom"+obj[i], Toast.LENGTH_SHORT).show();
                 arr.add(new PhoneList(da[i],danumber[i]));


            }
            ListView listView = (ListView) findViewById(R.id.lvContacts);
            CustomAdapter adpttt=new CustomAdapter(ContactList.this,R.layout.contacts_list_item,arr);
            listView.setAdapter(adpttt);
}
}

我的自定义适配器正在关注

public class CustomAdapter extends ArrayAdapter<PhoneList> {
    int inflatr;
    Context ctxt;
    ArrayList<PhoneList> data=new ArrayList<PhoneList>();
    public CustomAdapter(Context context, int resource, ArrayList<PhoneList> arr) {

        super(context, resource, arr);
        this.inflatr = resource;
        this.ctxt = context;
        this.data= arr;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserHolder holder = null;
         View row = convertView;
        if(convertView==null)
        {
             LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
             row = inflater.inflate(inflatr, parent, false);
             holder = new UserHolder();
             holder.textName=(TextView)row.findViewById(R.id.lblName);
             holder.stnumber=(TextView)row.findViewById(R.id.lblNumber);
//           holder.btnEdit = (ImageButton) row.findViewById(R.id.atomPay_removePay);
//           row.setTag(holder);
        }
        else
        {
             holder = (UserHolder) row.getTag();            
        }


         PhoneList dta=data.get(position);
        Toast.makeText(ctxt, "Humayoon    zoooommmm"+dta.getNumber(), Toast.LENGTH_SHORT).show();

        for(int i=1; i<=256; i++)
        {
            holder.textName.setText(dta.getName());
        }
        for(int j=1; j<=256; j++)
        {
            holder.textName.setText(dta.getNumber());
        }
//      holder.btnEdit.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//              Toast.makeText(ctxt, "Humayoon    Siddiqueeeeeeeeeeeeeeeeeee"+dta.getName(), Toast.LENGTH_SHORT).show();
//              Intent moreIntent=new Intent(getContext(),ContactList.class);
//              String tName=dta.getName();
//              moreIntent.putExtra("Template",tName);
//              v.getContext().startActivity(moreIntent);
//               // ctxt.startActivity(ctxt,ContactList.class);
//          }
//      });
        return row;
    }
    @Override
    public int getCount() {
//      // TODO Auto-generated method stub
        return data.size();
    }

    static class UserHolder {
        TextView textName;
        TextView textAddress;
        TextView textLocation;
        ImageButton btnEdit;
        Button btnDelete;
        TextView stnumber;
        }


}

我的Phonelist课程正在关注

package com.example.sqliteapplication;

    public class PhoneList {
        String name;
        String number;
        public PhoneList(String string,String number) {
            super();
            this.name = string;
            this.number=number;
        }
        public String getName() {
            return name;
            }
        public void setNumber(String number)
        {
            this.number=number;
        }
        public String getNumber()
        {
            return number;
        }
        public void setName(String name) {
            this.name = name;
            }



    }

错误

05-12 11:01:42.434: D/fghfh(2720): -1
05-12 11:01:42.451: E/CursorWindow(2720): Failed to read row 1, column -1 from a CursorWindow which has 2 rows, 34 columns.
05-12 11:01:42.452: D/AndroidRuntime(2720): Shutting down VM
05-12 11:01:42.452: W/dalvikvm(2720): threadid=1: thread exiting with uncaught exception (group=0x40f219a8)
05-12 11:01:42.465: E/AndroidRuntime(2720): FATAL EXCEPTION: main
05-12 11:01:42.465: E/AndroidRuntime(2720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqliteapplication/com.example.sqliteapplication.ContactList}: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.access$600(ActivityThread.java:156)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.os.Looper.loop(Looper.java:153)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.main(ActivityThread.java:5299)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at java.lang.reflect.Method.invoke(Method.java:511)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at dalvik.system.NativeStart.main(Native Method)
05-12 11:01:42.465: E/AndroidRuntime(2720): Caused by: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.database.CursorWindow.nativeGetString(Native Method)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.database.CursorWindow.getString(CursorWindow.java:434)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.database.CursorWrapper.getString(CursorWrapper.java:114)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at com.example.sqliteapplication.ContactList.onCreate(ContactList.java:47)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.Activity.performCreate(Activity.java:5122)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
05-12 11:01:42.465: E/AndroidRuntime(2720):     ... 11 more

2 个答案:

答案 0 :(得分:1)

我认为问题出在您的ContentResolver查询中进行投影您的查询​​中没有您的数字,请尝试:

String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID};

        mCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

答案 1 :(得分:0)

您可能已经添加了一个列,但测试设备仍然有一个没有该列的数据库文件。 只需卸载您的应用或清除其数据即可删除旧数据库文件,并使onCreate()在下次运行时重新创建数据库。

非常好重新安装应用