title: Can't get Value from RatingBar in Alert Dialog and Exception error (Android)
body: <p>I have got a alertdialog which shows rating bar, and creating a RatingBar dynamically and assigning it to an alert-dialog. .When I triggered onclick listener, it shows NullPointerException.
  How can I get the rating value inside the OK handler of alert dialog?
  I get a null pointer exception warning when I do this. Therefore, the question is how to get the rating value?</p>
  
  <p>thanks.</p>
  
  <pre><code>05-10 10:56:26.051: E/AndroidRuntime(8187):     FATAL EXCEPTION: main
  05-10 10:56:26.051: E/AndroidRuntime(8187):     Process: com.example.rsbuyclient, PID: 8187
  05-10 10:56:26.051: E/AndroidRuntime(8187):     java.lang.NullPointerException
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at com.example.rsbuyclient.User_menu$SearchResultAdapter$3.onClick(User_menu.java:304)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at android.view.View.performClick(View.java:4633)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at android.view.View$PerformClick.run(View.java:19330)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at android.os.Handler.handleCallback(Handler.java:733)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at android.os.Handler.dispatchMessage(Handler.java:95)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at android.os.Looper.loop(Looper.java:157)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at android.app.ActivityThread.main(ActivityThread.java:5356)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at java.lang.reflect.Method.invoke(Method.java:515)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
  05-10 10:56:26.051: E/AndroidRuntime(8187):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <hr>
  
  <p>java</p>
  
  <pre><code>                // custom dialog
              final Dialog dialog = new Dialog(context1);
              dialog.setContentView(R.layout.custom1);
              dialog.setTitle("Selection item");
  
              // set the custom dialog components - text, image and button
              TextView text = (TextView) dialog.findViewById(R.id.text);
              text.setText("Add item to Favorite or not ?");
              ImageView image = (ImageView) dialog.findViewById(R.id.image);
              image.setImageResource(R.drawable.ic_launcher);
  
              dialog_ratingbar = (RatingBar) findViewById(R.id.dialog_ratingbar);
  
              Button dialogButtonOK = (Button) dialog.findViewById(R.id.dialogButtonOK);              
              dialogButtonOK.setOnClickListener(new OnClickListener(){
                  @Override
                  public void onClick(View v) {                                               
                      // showRatingDialog();
  
                      dialog_ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
                      {
                          public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) 
                              {
                                  Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + String.valueOf(rating),Toast.LENGTH_LONG).show();
                              }
                      });
  
                      Toast.makeText(getApplicationContext(),"Add to my Favorite",Toast.LENGTH_SHORT).show();
                      dialog.dismiss();   
                  }                   
              });
  </code></pre>
  
  <hr>
  
  <p>The XML-layout</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:padding="2dip"&gt;  
  
  
  &lt;ImageView
      android:id="@+id/image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginRight="5dp" /&gt;
  
  &lt;TextView
      android:id="@+id/text"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:textColor="#000000" 
      android:layout_toRightOf="@+id/image"/&gt;/&gt;
  
  &lt;TextView android:text="Please give rate" android:id="@+id/rank_dialog_text1"
       android:textSize="24dp" android:layout_marginTop="10dp"
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:layout_gravity="center"/&gt;
  
   &lt;RatingBar 
       android:layout_marginTop="10dp" android:layout_marginBottom="10dp"
       android:layout_width="wrap_content" android:layout_height="wrap_content"
       android:clickable="true"
       android:id="@+id/dialog_ratingbar" android:layout_gravity="center"
       android:numStars="5" android:stepSize="1.0" android:rating="2"
       android:isIndicator="false"/&gt;
  
      &lt;LinearLayout
      android:id="@+id/linearLayout1"
      android:layout_width="266dp"
      android:layout_height="wrap_content" &gt;
  
   &lt;Button
       android:id="@+id/dialogButtonOK"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_margin="5dip"
       android:focusable="false"
       android:text="OK"
       android:textColor="#FFF8C6"
       android:textSize="20dip" /&gt;
  
    &lt;Button
        android:id="@+id/dialogButtonNO"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:focusable="false"
        android:text="NO"
        android:textColor="#FFF8C6"
        android:textSize="20dip" /&gt;
  </code></pre>
  
  <p>
  </p>