2017-03-08 16 views
0

我想让用户选择一个联系人并检索他的电话号码。我发现下面的代码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的消息:

11月三日至7日: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_MATCH

我试过l解决它,但找不到原因。 如果有人知道原因或可以提供不同的方式做到这一点,我会很高兴:)

谢谢!

+0

崩溃什么是U获得? –

+0

你必须在这里粘贴完整的堆栈跟踪。 – Yashasvi

+0

@Yashasvi如何获得完整的堆栈跟踪? –

回答

0

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