Android在对话框中放置RadioGroup出现的报错

Android中若把RadioGroup放置在自定义对话框中而且对RadioGroup做出监听就会出现空指针错误,

这是由于RadioGroup找不到自定义对话框的xml中的id造成的,若添加如下代码可以解决问题

[java]  view plain  copy
  1. RelativeLayout dialayout=(RelativeLayout)getLayoutInflater().inflate(R.layout.dialog, null);  

然后findViewById()方法要加上变量

[java]  view plain  copy
  1. RadioGroup rg=(RadioGroup)dialayout.findViewById(R.id.rg);  

其实不只是RadioGroup放置在自定义对话框中会出现问题,其他控件也会出现这个问题,都可以用上述方法解决

下面附赠一个方法,可以创建带有确定和取消按钮的对话框,以及对话框中对RadioGroup中的radioButton子元素进行判断

方法ple():

[java]  view plain  copy
  1. public void ple()  
  2. {  
  3.     int w_radioButton=null;  
  4.     RelativeLayout dialayout=(RelativeLayout)getLayoutInflater().inflate(R.layout.dialog, null);  
  5.     rg=(RadioGroup)dialayout.findViewById(R.id.rg);  
  6.     rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  7.           
  8.         @Override  
  9.         public void onCheckedChanged(RadioGroup arg0, int arg1) {  
  10.             // TODO Auto-generated method stub  
  11.                if(arg1==R.id.radioButton1){    
  12.                w_radioButton=1;  
  13.                }else   
  14.                 {  
  15.                 if(arg1==R.id.radioButton2){    
  16.                       w_radioButton=2;  
  17.                 }else{  
  18.                     if(arg1==R.id.radioButton3)  
  19.                     {  
  20.                          w_radioButton=3;  
  21.                     }  
  22.                     else  
  23.                     {  
  24.                         if(arg1==R.id.radioButton4)  
  25.                         {  
  26.                              w_radioButton=4;  
  27.                         }  
  28.                         else  
  29.                         {  
  30.                             if(arg1==R.id.radioButton5)  
  31.                             {  
  32.                                  w_radioButton=5;  
  33.                             }  
  34.                         }  
  35.                     }  
  36.                 }  
  37.                }  
  38.         }  
  39.     });  
  40.     new AlertDialog.Builder(this).setTitle("title").setIcon(R.drawable.time).setView(dialayout)  
  41.     .setPositiveButton("确定"new OnClickListener() {  
  42.           
  43.         @Override  
  44.         public void onClick(DialogInterface dialog,int which) {  
  45.         }  
  46.     })  
  47.     .setNegativeButton("取消"new OnClickListener() {  
  48.           
  49.         @Override  
  50.         public void onClick(DialogInterface dialog,int which) {  
  51.             // TODO Auto-generated method stub  
  52.               
  53.         }  
  54.     }).create().show();  
  55.   
  56. }  

下面是layout中的dialog.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.   
  7.     <RadioGroup  
  8.         android:id="@+id/rg"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:orientation="vertical"  
  13.         android:layout_alignParentTop="true"  
  14.         android:layout_margin="10dp" >  
  15.   
  16.         <RadioButton  
  17.             android:id="@+id/radioButton1"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:text="    111"  
  21.             android:textColor="#cbd5f3" />  
  22.   
  23.         <RadioButton  
  24.             android:id="@+id/radioButton2"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="    222"   
  28.             android:textColor="#cbd5f3"/>  
  29.   
  30.         <RadioButton  
  31.             android:id="@+id/radioButton3"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"  
  34.             android:text="    333"   
  35.             android:textColor="#cbd5f3"/>  
  36.   
  37.         <RadioButton  
  38.             android:id="@+id/radioButton4"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="    444"  
  42.             android:textColor="#cbd5f3" />  
  43.   
  44.         <RadioButton  
  45.             android:id="@+id/radioButton5"  
  46.             android:layout_width="wrap_content"  
  47.             android:layout_height="wrap_content"  
  48.             android:text="    555"  
  49.             android:textColor="#cbd5f3" />  
  50.     </RadioGroup>  
  51.   
  52. </RelativeLayout>  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值