获取android和java.lang.IllegalStateException中的联系号码

时间:2013-08-12 12:54:44

标签: android android-contacts

我尝试获取联系电话和姓名。我正确地说出了正确的名字但是当我试图得到数字时我得到了错误。

这是代码:

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

    ArrayList<String> contactList = new ArrayList<String>();

    switch (reqCode) {
    case (0):
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c = getContentResolver().query(contactData, null, null,
                    null, null);
            if (c.moveToFirst()) {
                String name = c
                        .getString(c
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


                String phoneNumber = c.getString(c
                        .getColumnIndex(Phone.NUMBER));

                String contact = name + "" + phoneNumber;

                contactList.add(contact);
            }
        }
        break;
    }

这是logCat:

08-12 16:25:19.790: E/WindowManager(14381): Activity tr.com.turkcell.shmobile.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@421c0090 that was originally added here

08-12 16:25:19.790:E / WindowManager(14381):android.view.WindowLeaked:Activity tr.com.turkcell.shmobile.MainActivity泄露窗口com.android.internal.policy.impl.PhoneWindow $ DecorView @ 421c0090最初是在这里添加的 08-12 16:25:19.790:E / WindowManager(14381):在android.view.ViewRootImpl。(ViewRootImpl.java:374) 08-12 16:25:19.790:E / WindowManager(14381):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292) 08-12 16:25:19.790:E / WindowManager(14381):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 08-12 16:25:19.790:E / WindowManager(14381):在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:149) 08-12 16:25:19.790:E / WindowManager(14381):在android.view.Window $ LocalWindowManager.addView(Window.java:549) 08-12 16:25:19.790:E / WindowManager(14381):在android.app.Dialog.show(Dialog.java:277) 08-12 16:25:19.790:E / WindowManager(14381):在android.app.ProgressDialog.show(ProgressDialog.java:116) 08-12 16:25:19.790:E / WindowManager(14381):在android.app.ProgressDialog.show(ProgressDialog.java:99) 08-12 16:25:19.790:E / WindowManager(14381):在android.app.ProgressDialog.show(ProgressDialog.java:94) 08-12 16:25:19.790:E / WindowManager(14381):at tr.com.turkcell.shmobile.MainActivity $ GetProduts.onPreExecute(MainActivity.java:188) 08-12 16:25:19.790:E / WindowManager(14381):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 08-12 16:25:19.790:E / WindowManager(14381):在android.os.AsyncTask.execute(AsyncTask.java:534) 08-12 16:25:19.790:E / WindowManager(14381):at tr.com.turkcell.shmobile.MainActivity $ 1 $ 1.run(MainActivity.java:59) 08-12 16:25:19.790:E / WindowManager(14381):在android.app.Activity.runOnUiThread(Activity.java:4591) 08-12 16:25:19.790:E / WindowManager(14381):at tr.com.turkcell.shmobile.MainActivity $ 1.onClick(MainActivity.java:56) 08-12 16:25:19.790:E / WindowManager(14381):在android.view.View.performClick(View.java:4084) 08-12 16:25:19.790:E / WindowManager(14381):在android.view.View $ PerformClick.run(View.java:16966) 08-12 16:25:19.790:E / WindowManager(14381):在android.os.Handler.handleCallback(Handler.java:615) 08-12 16:25:19.790:E / WindowManager(14381):在android.os.Handler.dispatchMessage(Handler.java:92) 08-12 16:25:19.790:E / WindowManager(14381):在android.os.Looper.loop(Looper.java:137) 08-12 16:25:19.790:E / WindowManager(14381):在android.app.ActivityThread.main(ActivityThread.java:4754) 08-12 16:25:19.790:E / WindowManager(14381):at java.lang.reflect.Method.invokeNative(Native Method) 08-12 16:25:19.790:E / WindowManager(14381):at java.lang.reflect.Method.invoke(Method.java:511) 08-12 16:25:19.790:E / WindowManager(14381):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833) 08-12 16:25:19.790:E / WindowManager(14381):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 08-12 16:25:19.790:E / WindowManager(14381):at dalvik.system.NativeStart.main(Native Method) 08-12 16:25:21.370:E / CursorWindow(14381):无法从包含1行29列的CursorWindow读取第0行第-1列。 08-12 16:25:21.370:D / AndroidRuntime(14381):关闭VM 08-12 16:25:21.370:W / dalvikvm(14381):threadid = 1:线程退出时未捕获异常(组= 0x41b6a318) 08-12 16:25:21.380:E / AndroidRuntime(14381):致命异常:主要 08-12 16:25:21.380:E / AndroidRuntime(14381):java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 0,result = -1,data = Intent {dat = content:// com.android.contacts / contacts / lookup / 209i1510398b8e8d8617 / 1 flg = 0x1}} to activity {tr.com.turkcell.shmobile / tr.com.turkcell.shmobile.IndexActivity}:java.lang.IllegalStateException:couldn&#39;从CursorWindow读取第0行,col -1。在从中访问数据之前,请确保正确初始化Cursor。 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.app.ActivityThread.deliverResults(ActivityThread.java:3150) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3193) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.app.ActivityThread.access $ 1100(ActivityThread.java:130) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1243) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.os.Handler.dispatchMessage(Handler.java:99) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.os.Looper.loop(Looper.java:137) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.app.ActivityThread.main(ActivityThread.java:4754) 08-12 16:25:21.380:E / AndroidRuntime(14381):at java.lang.reflect.Method.invokeNative(Native Method) 08-12 16:25:21.380:E / AndroidRuntime(14381):at java.lang.reflect.Method.invoke(Method.java:511) 08-12 16:25:21.380:E / AndroidRuntime(14381):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833) 08-12 16:25:21.380:E / AndroidRuntime(14381):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 08-12 16:25:21.380:E / AndroidRuntime(14381):at dalvik.system.NativeStart.main(Native Method) 08-12 16:25:21.380:E / AndroidRuntime(14381):引起:java.lang.IllegalStateException:无法从CursorWindow读取第0行,col -1。在从中访问数据之前,请确保正确初始化Cursor。 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.database.CursorWindow.nativeGetString(Native Method) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.database.CursorWindow.getString(CursorWindow.java:434) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.database.CursorWrapper.getString(CursorWrapper.java:114) 08-12 16:25:21.380:E / AndroidRuntime(14381):at tr.com.turkcell.shmobile.IndexActivity.onActivityResult(IndexActivity.java:85) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.app.Activity.dispatchActivityResult(Activity.java:5192) 08-12 16:25:21.380:E / AndroidRuntime(14381):在android.app.ActivityThread.deliverResults(ActivityThread.java:3146) 08-12 16:25:21.380:E / AndroidRuntime(14381):... 11更多 08-12 16:25:21.410:D / dalvikvm(14381):GC_CONCURRENT释放2769K,32%免费6571K / 9543K,暂停2ms + 4ms,总计37ms

1 个答案:

答案 0 :(得分:0)

你需要2个游标才能获得联系电话号码,为什么? ..假设一个联系人得到了多个你会做的数字:)? ....所以你需要一个光标来浏览联系人和一个光标来查看联系号码..就像@Piyush说的那样

ContentResolver cr = getContentResolver();

        Cursor cursor = cr.query(uri, projection, selection, null, sortOrder);
    while(cursor.moveToNext()){//this loops through contacts and put them in object ContactTest which i already made
        ContactTest c1 = new ContactTest();
        c1.setContactName(cursor.getString(/*name index*/));
        c1.setContactID(cursor.getString(/*id index you will need it*/));
        Cursor pCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {c1.getContactID()}, null);

        while(pCursor.moveToNext()){ // this loops through the contact number's

            c1.setContactPhone(pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        }
    }