【问题标题】:Get a contact phone number crash获取联系人电话号码崩溃
【发布时间】:2017-03-08 10:20:34
【问题描述】:

我正在尝试让用户选择一个联系人并检索他的电话号码。我找到了以下代码here

//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): error 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: Initialized EGL, version 1.4
03-07 11:16:53.265 5866-5883/com.example.roy.wakeapp E/EGL_emulation: tid 5883: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
03-07 11:16:53.265 5866-5883/com.example.roy.wakeapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad1ab480, error=EGL_BAD_MATCH

我试图查找它,但我找不到原因。如果有人知道原因或可以提供不同的方法,我将不胜感激。

【问题讨论】:

  • 你遇到了什么崩溃??
  • 您必须在此处粘贴完整的堆栈跟踪。
  • @Yashasvi 我如何获得完整的堆栈跟踪?
  • 我得到 E/EGL_emulation: tid 5883: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)。
  • 一旦发生崩溃,你会在 android studio logcat 中看到堆栈跟踪

标签: android


【解决方案1】:

尝试如下实例化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();
        }
    }

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 2015-01-24
    • 2016-12-26
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多