Android getContentResolver()。查询错误

Android getContentResolver()。查询错误,android,contacts,Android,Contacts,我对Java编程和Android开发都比较陌生,所以目前我的学习曲线相当陡峭。我似乎被困在一些我找不到像样的例子来说明如何克服它的事情上 我写了一个函数,根据我在网上找到的示例和文档获取我手机上的所有联系人。我似乎无法解决的问题是这个。下面的代码工作得很好 private void fillData() { // This goes and gets all the contacts // TODO: Find a way to filter this only on c

我对Java编程和Android开发都比较陌生,所以目前我的学习曲线相当陡峭。我似乎被困在一些我找不到像样的例子来说明如何克服它的事情上

我写了一个函数,根据我在网上找到的示例和文档获取我手机上的所有联系人。我似乎无法解决的问题是这个。下面的代码工作得很好

    private void fillData() {
    // This goes and gets all the contacts
    // TODO: Find a way to filter this only on contacts that have mobile numbers
    cursor = getContentResolver().query(Contacts.CONTENT_URI, null, null, null, null);

    final ArrayList<String> contacts = new ArrayList<String>();

    // Let's set our local variable to a reference to our listview control
    // in the view.
    lvContacts = (ListView) findViewById(R.id.lvContacts);

    while(cursor.moveToNext()) {
        contacts.add(cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)));
    }

    // Make the array adapter for the listview.
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_multiple_choice,
                                  contacts);

    // Let's sort our resulting data alphabetically.
    aa.sort(new Comparator<String>() {
        public int compare(String object1, String object2) {
            return object1.compareTo(object2);
        };
    });

    // Give the list of contacts over to the list view now.
    lvContacts.setAdapter(aa);
}
但当我这样做时,它会抛出一个null指针异常错误。这个怎么了? 我从android网站上的一个例子中得到了这一点,但是他们有一个where子句,不适用于我的需要,所以我将where内容更改为null。这就是把事情搞砸的原因吗


谢谢。

嗯,看来我已经自己找到了解决问题的办法。(在把头发从我头上拔下来,变成时髦的秃头之后)

似乎使用Phone.TYPE确实是个问题。Phone.TYPE是一个常量,不是数据列

事实证明,完美运行的代码是这样的

    private void fillData() {
    // This goes and gets all the contacts that have mobile numbers

    final ArrayList<String> contacts = new ArrayList<String>();

    // Let's set our local variable to a reference to our listview control
    // in the view.
    lvContacts = (ListView) findViewById(R.id.lvContacts);

    String[] proj_2    = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE};
    phnCursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null);
    while(phnCursor.moveToNext()) {
        if ( phnCursor.getInt(2) == Phone.TYPE_MOBILE ) {
            String name = phnCursor.getString(1);
            contacts.add(name);
        }
    }

    // Make the array adapter for the listview.
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_multiple_choice,
                                  contacts);

    // Let's sort our resulting data alphabetically.
    aa.sort(new Comparator<String>() {
        public int compare(String object1, String object2) {
            return object1.compareTo(object2);
        };
    });

    // Give the list of contacts over to the list view now.
    lvContacts.setAdapter(aa);
}
private void fillData(){
//这将获取所有具有手机号码的联系人
最终ArrayList联系人=新建ArrayList();
//让我们将局部变量设置为listview控件的引用
//在视图中。
lvContacts=(ListView)findViewById(R.id.lvContacts);
String[]proj_2=新字符串[]{Data.\u ID,Phone.DISPLAY\u NAME,commonDataTypes.Phone.TYPE};
phnCursor=managedQuery(Phone.CONTENT\u URI,项目2,null,null,null);
while(phnCursor.moveToNext()){
if(phnCursor.getInt(2)=Phone.TYPE\u MOBILE){
字符串名称=phnCursor.getString(1);
联系人。添加(姓名);
}
}
//为listview创建阵列适配器。
最终阵列适配器aa;
aa=新阵列适配器(此,
android.R.layout.simple\u list\u item\u多选,
接触);
//让我们按字母顺序对结果数据进行排序。
aa.排序(新比较器(){
公共整数比较(字符串对象1、字符串对象2){
返回object1.compareTo(object2);
};
});
//现在将联系人列表切换到列表视图。
LV触点。设置适配器(aa);
}

我感谢你的帮助,但不幸的是,一系列彻底的疯狂和研究最终取得了成效。希望这能帮助其他人。

问题似乎是它不喜欢新字符串[]{Data.\u ID,Phone.TYPE,Phone.LABEL}参数中的“data2”列引用。Phone.TYPE转换为“data2”,错误消息表示SQLiteQueryBuilder.computeProjection对该列不太满意。不知道如何解决这个问题。
    private void fillData() {
    // This goes and gets all the contacts that have mobile numbers

    final ArrayList<String> contacts = new ArrayList<String>();

    // Let's set our local variable to a reference to our listview control
    // in the view.
    lvContacts = (ListView) findViewById(R.id.lvContacts);

    String[] proj_2    = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE};
    phnCursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null);
    while(phnCursor.moveToNext()) {
        if ( phnCursor.getInt(2) == Phone.TYPE_MOBILE ) {
            String name = phnCursor.getString(1);
            contacts.add(name);
        }
    }

    // Make the array adapter for the listview.
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_multiple_choice,
                                  contacts);

    // Let's sort our resulting data alphabetically.
    aa.sort(new Comparator<String>() {
        public int compare(String object1, String object2) {
            return object1.compareTo(object2);
        };
    });

    // Give the list of contacts over to the list view now.
    lvContacts.setAdapter(aa);
}