Reputation: 5
I am new in android. i am trying to execute following code but the application crashes, In following code, if condition causes the nullpointerexception which is in bold text please check the following code...... I am sharing my code. thanks.....
private void showGroupChatDialog ()
{
ContentResolver cr = getContentResolver();
Imps.ProviderSettings.QueryMap settings = new Imps.ProviderSettings.QueryMap(
cr, mLastProviderId, false /* don't keep updated */, null /* no handler */);
String chatDomain = "conference." + settings.getDomain();
settings.close();
// This example shows how to add a custom layout to an AlertDialog
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_group_chat, null);
final TextView tvServer = (TextView) textEntryView.findViewById(R.id.chat_server);
tvServer.setText(chatDomain);
new AlertDialog.Builder(this)
.setTitle(R.string.create_or_join_group_chat)
.setView(textEntryView)
.setPositiveButton(R.string.connect, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
String chatRoom = null;
String chatServer = null;
int enablegrouphistory;
//int opentoall;
TextView tv = (TextView)textEntryView.findViewById(R.id.chat_room);
chatRoom = tv.getText().toString();
tv = (TextView) textEntryView.findViewById(R.id.chat_server);
chatServer = tv.getText().toString();
**CheckBox enablehistory = (CheckBox) findViewById(R.id.enable_group_history);
if (enablehistory.isChecked()) {
enablegrouphistory = 1;
}
CheckBox openall = (CheckBox) findViewById(R.id.open_to_all);
if(openall.isChecked()){
opentoall = 1;
}**
startGroupChat (chatRoom, chatServer, ((ImApp)getApplication()).getConnection(mLastProviderId));
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create().show();
}
Upvotes: 0
Views: 1069
Reputation: 47807
You should replace this
CheckBox enablehistory = (CheckBox) findViewById(R.id.enable_group_history);
CheckBox openall = (CheckBox) findViewById(R.id.open_to_all);
With
CheckBox enablehistory = (CheckBox) textEntryView.findViewById(R.id.enable_group_history);
CheckBox openall = (CheckBox) textEntryView.findViewById(R.id.open_to_all);
Upvotes: 1
Reputation: 133560
I guess the views belongs to alert_dialog_group_chat.xml
.
So change
CheckBox enablehistory = (CheckBox) findViewById(R.id.enable_group_history);
to
CheckBox enablehistory = (CheckBox) textEntryView.findViewById(R.id.enable_group_history);
Similarly
CheckBox openall = (CheckBox) textEntryView.findViewById(R.id.open_to_all);
You are getting NUllPointerException
as findViewById
looks for a view in the current inflated layout.
Upvotes: 1
Reputation: 13520
Change
CheckBox enablehistory = (CheckBox) findViewById(R.id.enable_group_history);
if (enablehistory.isChecked())
{
enablegrouphistory = 1;
}
CheckBox openall = (CheckBox) findViewById(R.id.open_to_all);
if(openall.isChecked())
{
opentoall = 1;
}
to
CheckBox enablehistory = (CheckBox) textEntryView.findViewById(R.id.enable_group_history);
if (enablehistory.isChecked())
{
enablegrouphistory = 1;
}
CheckBox openall = (CheckBox) textEntryView.findViewById(R.id.open_to_all);
if(openall.isChecked())
{
opentoall = 1;
}
You are inflating your layout in textEntryView
so you need to get your View
from there
Upvotes: 0
Reputation: 24853
Try this..
you have given TextView
like textEntryView.findViewById
same like that give both CheckBox
like textEntryView.findViewById
tv = (TextView) textEntryView.findViewById(R.id.chat_server);
CheckBox enablehistory = (CheckBox) textEntryView.findViewById(R.id.enable_group_history);
CheckBox openall = (CheckBox) textEntryView.findViewById(R.id.open_to_all);
Upvotes: 0