2012-04-04 87 views
0

我有一個顯示對話框的功能。無法獲取聯繫人電話簿列表

public Dialog sendSMS(){ 
     Dialog dialog = new Dialog(this); 
     dialog.setContentView(R.layout.dialogsms); 
     dialog.setTitle("Send SMS"); 
     dialog.setCancelable(true); 

     final Spinner spn = (Spinner)findViewById(R.id.spn_contatcs); 
     final TextView tenso = (TextView)findViewById(R.id.txt_phone); 

     final ArrayList<String> ten = new ArrayList<String>(); 
     final ArrayList<String> so = new ArrayList<String>(); 

     Cursor phones = _ketquatimkiem.this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
     while (phones.moveToNext()){ 
       String phoneName=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       ten.add(phoneName); 
       so.add(phoneNumber); 
      } 
     phones.close(); 

     ArrayAdapter<String> arrayAdapter_Contacts = new ArrayAdapter<String>(_ketquatimkiem.this,android.R.layout.simple_spinner_item,ten); 
     arrayAdapter_Contacts.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spn.setAdapter(arrayAdapter_Contacts); 
     spn.setOnItemSelectedListener(new OnItemSelectedListener(){ 
      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) { 
       tenso.setText("Phone Num: "+so.get(arg2).toString()); 
      } 
      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
      } 
     }); 
     return dialog; 
    } 

呼叫中的onCreate()

//when i click item of listview i get quickactiondialog  
listView.setOnItemClickListener(new OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 
     final QuickActionDialog quickAction = new QuickActionDialog(arg1.getContext(), QuickActionDialog.VERTICAL); 
     //when click item of quickactiondialog id == sms i show dialog 
     quickAction.setOnActionItemClickListener(new QuickActionDialog.OnActionItemClickListener() {   
      @Override 
      public void onItemClick(QuickActionDialog source, int pos, int actionId) {          
       if(actionId == ID_SMS){ 
        Dialog dialog= sendSMS(); 
        dialog.show(); 
       } 
      } 
     }); 

,但我得到的錯誤:E/AndroidRuntime(15562):顯示java.lang.NullPointerException spn.setAdapter(arrayAdapter_Contacts);

我在真實設備ss gt-5570上測試。對不起,我用英語不好:(

回答

1

在你sendSMS()方法,你可以訪問您的活動的佈局

final Spinner spn = (Spinner)findViewById(R.id.spn_contatcs); 

那一定是空,因爲你的活動佈局不包含微調。你必須這樣做以下(見content.findViewById

Dialog dialog = new Dialog(this); 
View content = View.inflate(this, R.layout.dialogsms, null); 
// your contact stuff 
Spinner spn = (Spinner) content.findViewById(R.id.spn_contatcs); 
spn.setAdapter(arrayAdapter_Contacts); 
dialog.setContentView(content); 
+0

謝謝。我沒有解決我的問題:d – HoangQBH 2012-04-04 16:57:04

相關問題