当在 android 中调用复选框的 isChecked() 时,它会给出空指针异常



我是安卓新手。 我正在尝试执行以下代码,但应用程序崩溃,在下面的代码中,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

试试这个。

你给了TextViewtextEntryView.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

最新更新