簡體   English   中英

取得聯絡電話號碼當機

[英]get a contact phone number crash

我試圖讓用戶選擇一個聯系人並檢索他的電話號碼。 我在https://developer.android.com中找到了以下代碼

//the onClick function for the user to pick his contact

public void pickContact(View view) {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
    }
}

//the function that should retrieve the phone number

@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();
        ContentResolver resolver = getContentResolver();

        String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursor = resolver.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);
            // Do something with the phone number
        }         
    }
}

我不知道為什么,但是當用戶選擇聯系人時,應用程序崩潰並顯示以下錯誤:E / EGL_emulation:tid 5883:eglSurfaceAttrib(1165):錯誤0x3009(EGL_BAD_MATCH)。 從調試來看,它似乎在此行崩潰:

Cursor cursor = resolver.query(contactUri,
                projection, null, null, null);

編輯:logcat消息:

03-07 11:16:53.239 5866-5883 / com.example.roy.wakeapp I / OpenGLRenderer:初始化的EGL版本1.4 03-07 11:16:53.265 5866-5883 / com.example.roy.wakeapp E / EGL_emulation :tid 5883:eglSurfaceAttrib(1165):錯誤0x3009(EGL_BAD_MATCH)03-07 11:16:53.265 5866-5883 / com.example.roy.wakeapp W / OpenGLRenderer:無法在表面0xad1ab480上設置EGL_SWAP_BEHAVIOR,錯誤= EGL_BAD_MAT

我試圖查找它,但找不到原因。 如果有人知道原因或可以提供其他方式,我會很高興的:)

謝謝!

嘗試如下實例化Intent

    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(intent, CONTACT_PICK);

和活動結果法

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CONTACT_PICK && 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 = getApplicationContext().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);

            }
            cursor.close();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM