2013-12-11 158 views
0

我使用此代碼打開我的聯繫人列表,然後選擇一個聯繫人以顯示在我的用戶界面中。我可以打開我的聯繫人列表,但是當我點擊任何聯繫人時,程序崩潰。打開手機聯繫人列表,然後選擇一個聯繫人

etphone1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      // Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      // intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
      // startActivityForResult(intent, 1); 

      Intent pb = new Intent(Intent.ACTION_PICK, 
        ContactsContract.Contacts.CONTENT_URI); 
      startActivityForResult(pb, 1); 

     } 
    }); 

onActivityForResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == 1) { 
     if (resultCode == RESULT_OK) { 
      Uri contactData = data.getData(); 
      Cursor cursor = managedQuery(contactData, null, null, null, 
        null); 
      cursor.moveToFirst(); 

      String number = cursor 
        .getString(cursor 
          .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      TextView tv; 
      tv = (TextView) findViewById(R.id.tv); 

      tv.setText(number); 

     } 
    } 

} 

的logcat:

12-11 11:05:48.969: E/AndroidRuntime(16292): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r10-272B2B434F414D37413143/10 flg=0x1 }} to activity {com.sriyank.locationmessenger/com.sriyank.locationmessenger.SecondPage}: java.lang.IllegalArgumentException: column 'data1' does not exist 
12-11 11:05:48.969: E/AndroidRuntime(16292): at android.app.ActivityThread.deliverResults(ActivityThread.java:3209) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3252) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at android.app.ActivityThread.access$1200(ActivityThread.java:143) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at android.os.Looper.loop(Looper.java:137) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at android.app.ActivityThread.main(ActivityThread.java:4960) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at java.lang.reflect.Method.invokeNative(Native Method) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at java.lang.reflect.Method.invoke(Method.java:511) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at dalvik.system.NativeStart.main(Native Method) 
12-11 11:05:48.969: E/AndroidRuntime(16292): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist 
12-11 11:05:48.969: E/AndroidRuntime(16292): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:302) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78) 
12-11 11:05:48.969: E/AndroidRuntime(16292): at com.sriyank.locationmessenger.SecondPage.onActivityResult(SecondPage.java:59) 
+0

哪裏是你的logcat的錯誤? – GrIsHu

+0

很難找到這個唯一的單詞'Crashing'的問題。請張貼您的日誌貓? –

+0

這裏是我的日誌貓: – user3080161

回答

1

以下代碼將3個文本視圖設置爲常用姓名和電話號碼。 設置您的按鈕點擊監聽器 如上所述設置意圖。

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
    switch (v.getId()) { 
    case R.id.l1: 

     startActivityForResult(intent, 1); 

     break; 
    case R.id.l2: 

     startActivityForResult(intent, 2); 

     break; 
    case R.id.l3: 

     startActivityForResult(intent, 3); 

     break; 

    } 
} 

這是onActivity結果

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if (data != null) { 
     Uri uri = data.getData(); 

     if (uri != null) { 
      Cursor c = null; 
      try { 
       c = getContentResolver() 
         .query(uri, 
           new String[] { 
             ContactsContract.CommonDataKinds.Phone.NUMBER, 
             ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }, 
           null, null, null); 

       if (c != null && c.moveToFirst()) { 
        String number = c.getString(0); 
        String Name = c.getString(1); 

        if (requestCode == 1) { 

         tvname1.setText(Name); 
         tvphone1.setText(number); 

        } else if (requestCode == 2) { 
         tvname2.setText(Name); 
         tvphone2.setText(number); 

        } else if (requestCode == 3) { 
         tvname3.setText(Name); 
         tvphone3.setText(number); 

        } 

       } 
      } finally { 
       if (c != null) { 
        c.close(); 
       } 
      } 
     } 

    } 

} 
1

不要初始化您的意見onActivityResult可能是問題會因爲你的Textview初始化。在你的onCreate()中聲明它。從下面的方法中刪除TextView初始化,並在onCreate()中進行初始化並檢查。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == 1) { 
     if (resultCode == RESULT_OK) { 
      Uri contactData = data.getData(); 
      Cursor cursor = managedQuery(contactData, null, null, null, 
        null); 
      cursor.moveToFirst(); 

      String number = cursor 
        .getString(cursor 
          .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      //TextView tv; 
      //tv = (TextView) findViewById(R.id.tv); //remove this lines. 

      tv.setText(number); 

     } 
    } 

} 

編輯:

試試用下面的代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.contact_picker); 

    // this opens the activity. note the Intent.ACTION_GET_CONTENT 
    // and the intent.setType 
    etphone1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser 
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE 
      intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
      startActivityForResult(intent, 1);     
     } 
    }); 
} 

現在,只要用戶選擇的聯繫人(也可能選擇幾個電話號碼之一)你可以用正常方式檢索數據:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (data != null) { 
     Uri uri = data.getData(); 

     if (uri != null) { 
      Cursor c = null; 
      try { 
       c = getContentResolver().query(uri, new String[]{ 
          ContactsContract.CommonDataKinds.Phone.NUMBER, 
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }, 
         null, null, null); 

       if (c != null && c.moveToFirst()) { 
        String number = c.getString(0); 
        String Name=c.getString(1); 
        Toast.makeText(this, number + Name, Toast.LENGTH_LONG).show();  
       } 
      } finally { 
       if (c != null) { 
        c.close(); 
       } 
      } 
     } 
    } 
} 
+0

嘗試過......但仍然無法正常工作 – user3080161

+0

查看我更新的答案並嘗試使用我提到過的代碼。 – GrIsHu

+0

你有沒有試過我的代碼?並確保您已在清單中提供了READ_CONTACT權限。 – GrIsHu

相關問題