我正在创建一个聊天应用程序,所以我需要实现广播功能,我已将AlertDialog放在另一个AlertDialog的按钮中 - 这样当用户想要添加联系人或从中选择所有联系人名称时它们被返回简单的游标适配器。
我的问题是,当单击联系人按钮时,ListView会正确显示联系人姓名,但当我单击“完成”或“全选”按钮时,它们会产生NullPointerException错误而不是检查的名称。请帮忙 - 提前谢谢。
下面是callBroadcast()函数的代码
void callBroadcast()
{
// get broadcast.xml view
LayoutInflater li = LayoutInflater.from(context);
View broadPop = li.inflate(R.layout.broadcast, null);
contacts = (Button) broadPop.findViewById(R.id.contacts);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(broadPop);
final EditText userInput = (EditText) broadPop.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder.setCancelable(false)
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// creating an alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// showing
alertDialog.show();
contacts.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
LayoutInflater li = LayoutInflater.from(context);
final View contactList = li.inflate(R.layout.be_sponge_contacts, null);
SimpleCursorAdapter listAdapter;
ListView contact_list = (ListView) findViewById( R.id.contactList );
String[] selectionArgs = null;
String[] projection = new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC";
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP+"='"+("1")+"'";
ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
String[] from = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
int[] to = new int[] {R.id.rowTextViewChecked};
listAdapter = new SimpleCursorAdapter(Chats.this, R.layout.simplerow_checked, cur, from, to);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setAdapter(listAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialogBuilder.setCancelable(false)
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
selected = (CheckBox) contactList.findViewById(R.id.rowTextViewChecked);
if (selected.isChecked())
{
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(Chats.this, name+" was chosen", Toast.LENGTH_SHORT).show();
}
}
})
.setNeutralButton("Select All", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
selected = (CheckBox) contactList.findViewById(R.id.rowTextViewChecked);
//setting all to be checked
selected.setChecked(true);
if (selected.isChecked())
{
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(Chats.this, name+" was chosen", Toast.LENGTH_SHORT).show();
selected.setChecked(true);
}
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// creating an alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// showing
alertDialog.show();
}
});
test = (TextView) broadPop.findViewById(R.id.namesChosen);
}