public?class?MainActivity?extends?AppCompatActivity?{
????private?TextView?et1,et2,et3;
????protected?void?onCreate(Bundle?savedInstanceState)?{
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.activity_main);
????????et1=(TextView)findViewById(R.id.id);
????????et2=(TextView)findViewById(R.id.displayname);
????????et3=(TextView)findViewById(R.id.number);
????????ContentResolver?cr=getContentResolver();
????????/*
???????????查詢:
??????????1.要查詢的URI,2.查詢的字段3.4.查詢的條件5.排序
????????*/
????????Cursor?c=cr.query(Contacts.CONTENT_URI,new?String[]{Contacts._ID,Contacts.DISPLAY_NAME},null,null,null);
?????????if(c!=null)
?????????{
?????????????while(c.moveToNext())
?????????????{
?????????????????int?id=c.getInt(c.getColumnIndex("Contacts._ID"));
?????????????????et1.setText(""+id);
?????????????????String?name=c.getString(c.getColumnIndex("Contacts.DISPLAY_NAME"));
?????????????????et2.setText(name);
?????????????????Cursor?c1=cr.query(Phone.CONTENT_URI,new?String[]{Phone.NUMBER,Phone.TYPE},Phone.CONTACT_ID+"="+id,null,null);
?????????????????if(c1!=null)
?????????????????{
?????????????????????while?(c1.moveToNext())?{
?????????????????????????int?type?=?c1.getInt(c1.getColumnIndex("Phone.TYPE"));
?????????????????????????if?(type?==?Phone.TYPE_HOME)?{
?????????????????????????????String?number?=?c1.getString(c1.getColumnIndex("Phone.NUMBER"));
?????????????????????????????et3.setText("家庭"+number);
?????????????????????????}
?????????????????????????if?(type?==?Phone.TYPE_MOBILE)?{
?????????????????????????????String?number?=?c1.getString(c1.getColumnIndex("Phone.NUMBER"));
?????????????????????????????et3.setText("手機"+number);
?????????????????????????}
?????????????????????}
?????????????????????c1.close();
?????????????????}
?????????????}
?????????????c.close();
?????????}
????}
}
把C1有關的代碼刪除后就可以運行了,,求看錯誤
2016-10-17
這個錯誤應該是選擇的列于表中的列不匹配,把getColumnIndex中的引號去掉應該就可以了,加了引號和不加引號區別很大。