Android的复选框,单选按钮组自定义对话框 [英] android check box and radio group on custom dialog

查看:246
本文介绍了Android的复选框,单选按钮组自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我已经写了简单的自定义对话框。其中有几个检查boxs和一个submitt按钮。

每当我试图读取该复选框apllication抛出空指针异常..有人可以帮助解决这个问题,下面是我的自定义dailog code

 公共无效的onClick(查看为arg0){            //定制对话框
            最后对话的对话=新的对话框(背景);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle(标题...);
             复选框CHK1 =(复选框)findViewById(R.id.chkbox1);
               按钮dialogBu​​tton =(按钮)dialog.findViewById(R.id.dialogBu​​ttonOK);
            //如果点击按钮,关闭对话框定制
            dialogBu​​tton.setOnClickListener(新OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                   如果(chk1.isChecked())
                    dialog.dismiss();
                }
            });            dialog.show();
}


解决方案

NullPointerException异常,因为你没有用 dialog.findViewById实例化()并设置 OnClickListener 的复选框。把它作为如下:

 复选框CHK1 =(复选框)dialog.findViewById(R.id.chkbox1);
 chk1.setOnClickListener(新OnClickListener(){   //这里做什么
  });

Hi I have written simple custom dialog . which has few check boxs and one submitt button .

whenever I tried to read the checkbox apllication throws Nullpointer exception .. can somebody helps to solve this , below is my custom dailog code

    public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");
             CheckBox chk1= (CheckBox) findViewById(R.id.chkbox1);
               Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                   if(chk1.isChecked())
                    dialog.dismiss();
                }
            });

            dialog.show();
}

解决方案

NullPointerException because you didn't instantiated with dialog.findViewById() and set OnClickListener for the CheckBox. Place it as below:

 CheckBox chk1= (CheckBox)dialog.findViewById(R.id.chkbox1);
 chk1.setOnClickListener(new OnClickListener() {

   //do something here
  });

这篇关于Android的复选框,单选按钮组自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆