Android 为什么RadioButton返回空值?

Android 为什么RadioButton返回空值?,android,radio-button,radio-group,Android,Radio Button,Radio Group,我在RadioGroup中有一个与4个RadioButtons的对话框,我试图返回单击的按钮的ID,但我一直得到一个NullPointerException。我看了几个例子,看不出我的有什么不同 <RadioGroup android:id="@+id/radiojqmobdiv" android:layout_width="match_parent" android:layout_height="wra

我在
RadioGroup
中有一个与4个
RadioButton
s的对话框,我试图返回单击的按钮的ID,但我一直得到一个
NullPointerException
。我看了几个例子,看不出我的有什么不同

    <RadioGroup
             android:id="@+id/radiojqmobdiv"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:checkedButton="0"
             android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/jqpage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/jqpage" 
                android:checked="true" />

            <RadioButton
                android:id="@+id/jqheader"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/jqheader" />

            <RadioButton
                android:id="@+id/jqcontent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/jqcontent" />

            <RadioButton
                android:id="@+id/jqfooter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/jqfooter" />

        </RadioGroup>

代码:

@覆盖
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.main);
首选项=GetSharedReferences(“MYPREFS”,0);
et=(editTextLineNumber)findViewById(R.id.ide);
et.setTextColor(preferences.getInt(“colorChoice”,Color.GREEN));
et.setMaxLines(5000);
divBtn=(按钮)findviewbyd(R.id.divbutton);
divGroup=(放射组)findViewById(R.id.radiojqmobdiv);
存在=假;
gestureDetector=新的gestureDetector(这个,新的MyGestureDetector());
gestureListener=新建视图。OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
返回gestureDetector.onTouchEvent(事件);
}
};
web=(WebView)findviewbyd(R.id.webpreview);
setOnClickListener(this);
setOnTouchListener(gestureListener);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT\u输入\u状态\u隐藏);
File dir=新文件(Environment.getExternalStorageDirectory()
+“/我的网”);
currentDirectory=dir;
ListView lv=getListView();
registerForContextMenu(lv);
lv.setOnItemLongClickListener(新的OnItemLongClickListener(){
@凌驾
长按(适配器视图a、视图v、,
内部位置,长id){
显示警告框(v.getContext(),“请选择操作”,
职位);
返回false;
}
});
et.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列,int start,int before,
整数计数){
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
}
@凌驾
公共无效后文本已更改(可编辑){
更改=正确;
startPos=et.getSelectionStart();
endPos=et.getSelectionEnd();
}
});
if(dir.isDirectory()){
browseToRoot();
}否则{
dir.mkdir();
}
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
最终对话框=新对话框(MainActivity.this);
setContentView(R.layout.divdialog);
对话框.setTitle(“Inserv Div”);
//设置自定义对话框组件-文本、图像和按钮
//TextView text=(TextView)dialog.findViewById(R.id.text);
//setText(“Android自定义对话框示例!”);
//ImageView图像=(ImageView)
//dialog.findviewbyd(R.id.image);
//setImageResource(R.drawable.ic_启动器);
**按钮插入按钮=(按钮)对话框
.findviewbyd(R.id.insertBtn)**
//如果单击按钮,则关闭自定义对话框
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//int selectedID=divGroup.getCheckedRadioButtonId();
//通过返回的id查找radiobutton
divRdoBtn=(单选按钮)findViewById(selectedID);
Toast.makeText(MainActivity.this,divRdoBtn.getText(),
Toast.LENGTH_LONG).show();
}
});
按钮取消按钮=(按钮)对话框
.findviewbyd(R.id.cancelBtn);
cancelButton.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
dialog.dismise();
}
});
dialog.show();
}
});
gestureDetector=新的gestureDetector(这个,新的MyGestureDetector());
gestureListener=新建视图。OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
返回gestureDetector.onTouchEvent(事件);
}
};
loadPrefs();
}

您没有发布对话框布局和活动布局的完整xml文件,因此我在这里做一些假设

但是,您在标题中说,对话框包含您命名为divGroup的RadioGroup。如果这是真的,那么当您执行这一行时,您将无法找到RadioGroup是有意义的:

divGroup = (RadioGroup) findViewById(R.id.radiojqmobdiv);
您正在活动中搜索id R.id.radiojqmobdiv,您暗示该id仅存在于尚未创建的对话框中。如果找不到视图,findViewById()将返回null,因此您将null分配给
divGroup

声明对话框后,应向下移动该行,并确保在
对话框
对象上而不是在活动上调用
findViewById

final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.divdialog);
divGroup = (RadioGroup) dialog.findViewById(R.id.radiojqmobdiv);

divGroup在创建()时初始化;NullException发生在int selectedID上…然后
divGroup
null
是否需要通过编程方式将单选按钮添加到组中?我明白了。我假设您在
divGroup=(RadioGroup)findViewById(R.id.radiojqmobdiv)之前调用
setContentView()
setContentV中的
布局
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.divdialog);
divGroup = (RadioGroup) dialog.findViewById(R.id.radiojqmobdiv);