我是安卓新手。 我正在尝试执行以下代码,但应用程序崩溃,在下面的代码中,if 条件导致粗体文本中的空指针异常请检查以下代码...我正在分享我的代码。谢谢。。。。。
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();
}
你应该替换这个
CheckBox enablehistory = (CheckBox) findViewById(R.id.enable_group_history);
CheckBox openall = (CheckBox) findViewById(R.id.open_to_all);
跟
CheckBox enablehistory = (CheckBox) textEntryView.findViewById(R.id.enable_group_history);
CheckBox openall = (CheckBox) textEntryView.findViewById(R.id.open_to_all);
我想这些观点属于alert_dialog_group_chat.xml
.
所以改变
CheckBox enablehistory = (CheckBox) findViewById(R.id.enable_group_history);
自
CheckBox enablehistory = (CheckBox) textEntryView.findViewById(R.id.enable_group_history);
同样地
CheckBox openall = (CheckBox) textEntryView.findViewById(R.id.open_to_all);
当您findViewById
在当前膨胀的布局中查找视图时,您会NUllPointerException
。
试试这个。
你给了TextView
像textEntryView.findViewById
一样,给了两个CheckBox
喜欢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);
更改
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;
}
自
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;
}
您正在textEntryView
中夸大布局,因此您需要从那里获取View