我有一个显示Dialog的功能。
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 at spn.setAdapter(arrayAdapter_Contacts);
我在真实设备上测试ss gt-5570。对不起,我使用英语不好:(
答案 0 :(得分:1)
在sendSMS()方法中,您正在访问活动的布局
final Spinner spn = (Spinner)findViewById(R.id.spn_contatcs);
必须为null,因为您的Activity布局不包含Spinner。您必须执行以下操作(请参阅 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);