Question

I want to be able to off the user a choice of options and would like to off er some radio buttons within a dialog box. I have declared the radio buttons like this in the OnCreate section

@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) {
             }

The radio_listener procedure is declared like this

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();         
      }
};

However when the dialog is called, I get an Null exception error on this line

radio_date.setOnClickListener(radio_listener);

What am I doing wrong?

Was it helpful?

Solution

When you want to get the ID of a UI element that is located within a dialog box you need to do it like this.

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

If you use findViewById then you are trying to capture the view object that is associated with the current acitivity view (which you would have set in the setContentView API)

So it is trying to find a view with the id RBdate in the activity view which it is not able to find and hence returns a null.

OTHER TIPS

check the following code This is an alert with radio buttons on a click of button

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();
        }
    });

u try this and let me know

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top