我是android新手。我试图执行以下代码但应用程序崩溃, 在下面的代码中,如果condition导致nullpointerexception,它是粗体文本 请检查以下代码...... 我正在分享我的代码。感谢.....
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();
}
答案 0 :(得分:1)
我猜视图属于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);
当NUllPointerException
找到当前虚增布局中的视图时,您将获得findViewById
。
答案 1 :(得分:1)
你应该替换这个
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);
答案 2 :(得分:0)
试试这个..
您提供的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);
{{1}}
答案 3 :(得分:0)
更改
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