0

ユーザーがオプションを選択できるようにしたいと考えており、ダイアログ ボックス内でいくつかのラジオ ボタンを提供したいと考えています。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();         
      }
};

ただし、ダイアログが呼び出されると、この行で Null 例外エラーが発生します

radio_date.setOnClickListener(radio_listener);

私は何を間違っていますか?

4

2 に答える 2

1

ダイアログボックス内にあるUI要素のIDを取得する場合は、次のようにする必要があります。

  Dialog dialog = new Dialog(mContext); // your dialog creation method here
  RadioButton radio_date = (RadioButton) dialog.findViewById(R.id.RBdate);

を使用する場合は、現在のアクティビティビュー( APIfindViewByIdで設定したもの)に関連付けられているビューオブジェクトをキャプチャしようとしています。setContentView

そのため、アクティビティビューでIDを持つビューを見つけようとしていますが、RBdate見つけることができず、nullを返します。

于 2012-02-24T10:59:41.890 に答える
0

次のコードを確認してくださいこれは、ボタンをクリックするだけでラジオボタンが表示されるアラートです

final AlertDialog.Builder  builder = new AlertDialog.Builder(this);

btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            //int states = {false, false};
            builder.setTitle("Select the item  that you want to delete from that item ");
            builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {

                    // TODO Auto-generated method stub
                    if(id==0){

                    }
                    else
                    {

            });

            alert= builder.create();
            alert.show();
        }
    });

これを試して、私に知らせてください

于 2012-02-24T10:59:04.470 に答える