我在RadioButton
中有4个RadioGroup
的对话框,我正在尝试返回点击按钮的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>
代码:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
preferences = getSharedPreferences("MYPREFS", 0);
et = (EditTextLineNumbers) findViewById(R.id.ide);
et.setTextColor(preferences.getInt("colourChoice", Color.GREEN));
et.setMaxLines(5000);
divBtn = (Button) findViewById(R.id.divbutton);
divGroup = (RadioGroup) findViewById(R.id.radiojqmobdiv);
exists = false;
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
web = (WebView) findViewById(R.id.webpreview);
web.setOnClickListener(this);
web.setOnTouchListener(gestureListener);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
File dir = new File(Environment.getExternalStorageDirectory()
+ "/My Webs");
currentDirectory = dir;
ListView lv = getListView();
registerForContextMenu(lv);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> a, View v,
int position, long id) {
Show_Alert_box(v.getContext(), "Please select action.",
position);
return false;
}
});
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
changed = true;
startPos = et.getSelectionStart();
endPos = et.getSelectionEnd();
}
});
if (dir.isDirectory()) {
browseToRoot();
} else {
dir.mkdir();
}
divBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.divdialog);
dialog.setTitle("Inserv Div");
// set the custom dialog components - text, image and button
// TextView text = (TextView) dialog.findViewById(R.id.text);
// text.setText("Android custom dialog example!");
// ImageView image = (ImageView)
// dialog.findViewById(R.id.image);
// image.setImageResource(R.drawable.ic_launcher);
**Button insertButton = (Button) dialog
.findViewById(R.id.insertBtn);**
// if button is clicked, close the custom dialog
insertButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// int selectedID = divGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
divRdoBtn = (RadioButton) findViewById(selectedID);
Toast.makeText(MainActivity.this, divRdoBtn.getText(),
Toast.LENGTH_LONG).show();
}
});
Button cancelButton = (Button) dialog
.findViewById(R.id.cancelBtn);
cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
loadPrefs();
}
答案 0 :(得分:1)
您没有发布对话框布局和活动布局的完整xml文件,所以我在这里做了一些假设。
但是,您在标题中说对话包含您命名为divGroup的RadioGroup。如果这是真的,那么在执行这一行时你将无法找到RadioGroup:
divGroup = (RadioGroup) findViewById(R.id.radiojqmobdiv);
您正在活动中搜索id为R.id.radiojqmobdiv,您隐含的内容仅存在于尚未创建的对话框中。如果找不到视图,findViewById()将返回null,因此您将null赋给divGroup
。
您应该在声明对话框后将该行向下移动,并确保在findViewById
对象而不是活动上调用dialog
。
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.divdialog);
divGroup = (RadioGroup) dialog.findViewById(R.id.radiojqmobdiv);