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

时间:2014-02-19 05:57:00

标签: android checkbox

我是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();

    }

4 个答案:

答案 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.findViewByIdCheckBox一样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