我有一个 4RadioButton
秒的对话框,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();
}