2013-04-12 42 views
-1
  • 我已經在我的Activity.Its中使用挑選聯繫人功能,可以正常工作來選擇聯繫人以及其他詳細信息,如姓名。
  • 但是,當我點擊按鈕它去接觸意圖和顯示數量一切都很好。
  • 但是,如果我沒有選擇號碼並點擊後按意味着應用程序正在關閉。

代碼背壓力關閉

Intent inten = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(inten,PICK_CONTACT); 

@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data) { 
    super.onActivityResult(reqCode, resultCode, data); 

    Cursor c; 
    switch (reqCode) { 
    case (PICK_CONTACT): 

     Uri result = data.getData(); 
    Log.v("TAG", "Got a result: " + result.toString()); 

    // get the contact id from the Uri 
    String id = result.getLastPathSegment(); 

    // query for phone numbers for the selected contact id 
    c = getContentResolver().query(
      Phone.CONTENT_URI, null, 
      Phone.CONTACT_ID + "=?", 
      new String[]{id}, null); 

    int phoneIdx = c.getColumnIndex(Phone.NUMBER); 
    int phoneType = c.getColumnIndex(Phone.TYPE); 

    if(c.getCount() > 1) { // contact has multiple phone numbers 
     final CharSequence[] numbers = new CharSequence[c.getCount()]; 
     int i=0; 
     if(c.moveToFirst()) { 
      while(!c.isAfterLast()) { // for each phone number, add it to the numbers array 
       String type = (String) Phone.getTypeLabel(this.getResources(), c.getInt(phoneType), ""); // insert a type string in front of the number 
       String number = type + ": " + c.getString(phoneIdx); 
       numbers[i++] = number; 
       c.moveToNext(); 
      } 
      // build and show a simple dialog that allows the user to select a number 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("Select Number"); 
      builder.setItems(numbers, new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int item) { 
        String number = (String) numbers[item]; 
        int index = number.indexOf(":"); 
        number = number.substring(index + 2); 
        cNumber=number; 
        edt.setText(cNumber); 
        //Toast.makeText(getApplicationContext(), number, Toast.LENGTH_LONG).show(); 
        // loadContactInfo(number); // do something with the selected number 
       } 
      }); 
      AlertDialog alert = builder.create(); 
      alert.setOwnerActivity(this); 
      alert.show(); 

     } else Log.w("TAG", "No results"); 
    } else if(c.getCount() == 1) { 
     // contact has a single phone number, so there's no need to display a second dialog 

     if(c.moveToFirst()) 
     { 
      cNumber = c.getString(c.getColumnIndex("data1")); 
      edt.setText(cNumber); 
      //Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_LONG).show(); 
     } 
    } 

    break; 
    } 

} 

在我的活動我已經寫回按還...

@Override 
public void onBackPressed() { 

    btnflag++; 
    if(btnflag==1) 
    { 
     Intent mainscr = new Intent(FindList1.this, MainScreen.class); 
     startActivity(mainscr); 
     btnflag=0; 
     finish(); 
    } 
} 

但不幸的是,碰撞發生.....

+10

請寄出logcat。 – Egor

+1

例外! LogCat? – Prateek

+0

您需要查看結果代碼。很明顯,請求代碼將始終是相同的。無論用戶是否選擇了聯繫人,您都在處理您的代碼。 –

回答

1

當用戶點擊返回按鈕時,它會調用activityResult()。我猜你在這段代碼中得到空指針異常。

Uri result = data.getData(); 
Log.v("TAG", "Got a result: " + result.toString()); 

// get the contact id from the Uri 
String id = result.getLastPathSegment(); 

// query for phone numbers for the selected contact id 
c = getContentResolver().query(
Phone.CONTENT_URI, null, 
Phone.CONTACT_ID + "=?", 
new String[]{id}, null); 

int phoneIdx = c.getColumnIndex(Phone.NUMBER); 
int phoneType = c.getColumnIndex(Phone.TYPE); 

data.getData()它將返回null。因爲您未在通訊錄活動中選擇任何聯繫人和您的後退按鈕。

if(data==null){ return; }

和處理該異常

onBackPressed()時當前活動被按下後退按鈕方法將調用。當你在其他活動中時,它不會打電話給你。

+0

感謝您的ideas.its正常工作...... –