无法解析方法"managedQuery" [英] Cannot resolve method 'managedQuery"

查看:87
本文介绍了无法解析方法"managedQuery"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个独立的应用程序,可以提取联系人的电话号码并将其填写到文本框中.作为一项活动,它可以很好地工作,但是当我将相同的代码放入片段中时,出现一个错误,即它无法解析方法"managedQuerry".我也收到有关getContentResover()的消息.

I have a stand alone app that pulls up the phone number of a contact and fills it into a text box. This works fine as an activity, but when I put the same code into a fragment, I get an error that it cannot resolve the method 'managedQuerry'. I also get the message for getContentResover().

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    try {
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cur = managedQuery(contactData, null, null, null, null);
            ContentResolver contect_resolver = getContentResolver();

            if (cur.moveToFirst()) {
                String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                String name = "";
                String no = "";

                Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

                if (phoneCur.moveToFirst()) {
                    name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }

                Log.e("Phone no & name :***: ", name + " : " + no);
                //txt.append(name + " : " + no + "\n");

                editPhoneNum.setText(no);

                id = null;
                name = null;
                no = null;
                phoneCur = null;
            }
            contect_resolver = null;
            cur = null;
            //                      populateContacts();
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        Log.e("IllegalArgException :: ", e.toString());
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("Error :: ", e.toString());
    }
}

推荐答案

我遇到了与您相同的问题.

I had the same problem as you.

这是我项目的对话框,用于获取联系人信息并显示给他们

This is a dialog of my project that get contacts information and show them

 private void contactsdialog() {
        final Dialog dialog = new Dialog(getContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.contactsdialog);
        ListView contacts_list = (ListView)dialog.findViewById(R.id.listView);
        Uri uri = ContactsContract.Contacts.CONTENT_URI;

        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.PHOTO_ID
        };

        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        Cursor cursor =dialog.getContext().getContentResolver().query(uri, projection, null, null, sortOrder);

        ArrayList<String> columnArray1 = new ArrayList<String>();
        ArrayList<String> columnArray2 = new ArrayList<String>();

        while (cursor.moveToNext()) {
            try {
                columnArray2.add(cursor.getString(0));
                columnArray1.add(cursor.getString(1));
            } catch(Exception e) {
                // Log.d("error",e.toString());
            }
        }

        cursor.close();
        String[] colStrArr1 = (String[]) columnArray1.toArray(new String[columnArray1.size()]);
        String[] colStrArr2 = (String[]) columnArray2.toArray(new String[columnArray1.size()]);

        contactadapter adapter = new contactadapter(getActivity(),colStrArr1,colStrArr2);
        contacts_list.setAdapter(adapter);

        dialog.show();
    } 

如您所见,我不使用ManagedQuery,因为它已被弃用.相反,我使用了与托管查询相同的"getContentResolver().query".另外,您还需要一个对话框名称(如我的代码中的对话框")将它与getContentResolver().query绑定(因为它不在活动中,并且我的主类是类似于您的情况的片段).对不起,英语不好

As you can see, I didn't use managedquery, because it's deprecated. Instead I used "getContentResolver().query" that is the same as managedquery. Also you need a dialog name like "dialog in my codes" to bound getContentResolver().query with it (because its not on a activity and my main class is fragment like your case). And sorry for bad English

这篇关于无法解析方法"managedQuery"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆