我希望能夠關閉用戶選項並希望關閉對話框中的某些單選按鈕。我宣佈這樣的單選按鈕在OnCreate部分在對話框中使用單選按鈕
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
if (id > 0)
{
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = layoutInflater.inflate(R.layout.lectsort_dialog, (ViewGroup) findViewById(R.id.lect_sort));
builder.setView(layout);
// Now configure the AlertDialog
builder.setTitle(R.string.exsort_title);
radio_date = (RadioButton) findViewById(R.id.RBdate);
radio_loctn = (RadioButton) findViewById(R.id.RBloctn);
radio_stream = (RadioButton) findViewById(R.id.RBstream);
radio_date.setOnClickListener(radio_listener);
radio_loctn.setOnClickListener(radio_listener);
radio_stream.setOnClickListener(radio_listener);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
的radio_listener過程聲明這樣
RadioButton.OnClickListener radio_listener =
new RadioButton.OnClickListener()
{
@Override
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(LecturesActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
然而,當對話框被調用時,我得到一個空異常錯誤在此行
radio_date.setOnClickListener(radio_listener);
我在做什麼錯?
感謝您的回覆,我敢肯定這是正確的。不過,我已經添加了以下代碼Context mContext = getApplicationContext();對話框=新對話框(mContext); RadioButton radio_date =(RadioButton)dialog.findViewById(R.id.RBdate);但是我仍然得到一個空異常錯誤,我是否在錯誤的地方宣佈它? – user616076 2012-02-24 11:21:47
你確定你在相同的地方(對於RBdate)得到異常嗎?或用於不同的單選按鈕。在調試模式下運行你的應用程序並逐步找出確切的位置 – bluefalcon 2012-02-24 11:33:14
是的,我在這裏得到錯誤 - radio_date.setOnClickListener(radio_listener);如果我嘗試不同的方式,我得到這裏的錯誤 - 如果(radio_date.isChecked()) – user616076 2012-02-24 11:41:09