无法获取电话号码并显示在Toast对象上

时间:2017-03-26 16:42:13

标签: java android

我试图在toast对象的联系人列表中显示选择的联系人,我想显示名称和号码,但是当我选择联系人时我的应用程序停止工作,我不知道为什么。< / p>

这是我的代码:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
            // Get the URI and query the content provider for the phone number
            Uri contactUri = data.getData();
            String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
            Cursor cursor = getContentResolver().query(contactUri, projection,
                    null, null, null);
            // If the cursor returned is valid, get the phone number
            if (cursor != null && cursor.moveToFirst()) {
                int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                String number = cursor.getString(numberIndex);
                String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));                // Do something with the phone number
                Context context = getApplicationContext();
                CharSequence text = "Hello toast!";
                int duration = Toast.LENGTH_SHORT;
                String fullContact = contactName + ": " + number;

                Toast toast = Toast.makeText(context,fullContact, duration);
                toast.show();
            }
        }
    }

StrackTrace

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.filipecosta.mytasks, PID: 3554
                  java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r2-2D3F29512F3929/2 flg=0x1 }} to activity {com.example.filipecosta.mytasks/com.example.filipecosta.mytasks.TaskActivity}: java.lang.IllegalArgumentException: Invalid column data1
                      at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
                      at android.app.ActivityThread.access$1300(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.IllegalArgumentException: Invalid column data1
                      at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
                      at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
                      at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                      at android.content.ContentResolver.query(ContentResolver.java:478)
                      at android.content.ContentResolver.query(ContentResolver.java:422)
                      at com.example.filipecosta.mytasks.TaskActivity.onActivityResult(TaskActivity.java:64)
                      at android.app.Activity.dispatchActivityResult(Activity.java:6192)
                      at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) 
                      at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

0 个答案:

没有答案