Search code examples
androiddialograting

Android Ratingbar in custom dialog it's not working, in debugger shows rankingbar:null(value)?


public static float i=0;
public static String tst;
private RatingBar ratingBar;
private  Dialog dialog;
private  Button dialog_btn;

//here goes onCreate metod etc..

protected Dialog onCreateDialog(int id)
    {  
      dialog = new Dialog(this); 
      dialog.setContentView(R.layout.customdialog); 
      dialog.setTitle("Рејтинг"); 

      ratingBar = (RatingBar) findViewById(R.id.ratingbarD);
      dialog_btn = (Button) dialog.findViewById(R.id.rank_dialog_button); 


      dialog_btn.setOnClickListener(new View.OnClickListener()  
      {

        public void onClick(View arg0) {

            i=ratingBar.getRating();
                 tst=Float.toString(i); 
                Toast.makeText(vicp.this,tst,Toast.LENGTH_SHORT).show(); 
                dialog.dismiss();

        } 

      });
                return dialog;

    }

//i have xml for customdialog and on it rating bar and button, the error i get is on line(i=ratingbar.getRating();)


Solution

  • Didn't you forget to do the findViewById() on the dialog? In other words, shouldn't it be:

    ratingBar = (RatingBar) dialog.findViewById(R.id.ratingbarD);