Can't get Value from RatingBar in Alert Dialog and Exception error (Android)

StackOverflow https://stackoverflow.com/questions/23576645

  •  19-07-2023
  •  | 
  •  

Вопрос

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?

thanks.

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)

java

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

The XML-layout

<?xml version="1.0" encoding="utf-8"?>
<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">  


<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000" 
    android:layout_toRightOf="@+id/image"/>/>

<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"/>

 <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"/>

    <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="266dp"
    android:layout_height="wrap_content" >

 <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" />

  <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" />

Это было полезно?

Решение

First Change it

dialog_ratingbar = (RatingBar) findViewById(R.id.dialog_ratingbar);

to

dialog_ratingbar = (RatingBar) dialog.findViewById(R.id.dialog_ratingbar);

//And try like this to get the value of Rating Bar on On Button Click.

 String answerValue =null;
    Button dialogButtonOK = (Button) dialog.findViewById(R.id.dialogButtonOK); 

    dialog_ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
                            public void onRatingChanged(RatingBar ratingBar,
                                    float rating, boolean fromUser) {
        answerValue = String.valueOf(ratingBar.getRating());// Get the Rating Here
                            }
                        });            
    dialogButtonOK.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View v) {                                               
                        Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + answerValue,Toast.LENGTH_LONG).show();
                        dialog.dismiss(); 
                    }                   
                });

Другие советы

Your rating dialog is part of your dialog so you should use dialog.findViewById to get the value of the ratingBar.

Change the related line to the following:

...
dialog_ratingbar = (RatingBar) dialog.findViewById(R.id.dialog_ratingbar);
...

hope this solve your problem.

you are getting rating bar because you are using findViewById of activity class

dialog_ratingbar = (RatingBar) dialog.findViewById(R.id.dialog_ratingbar);

as well, If you want rating value on button click then you dont have to register for setOnRatingBarChangeListener as you can directly get value from rating bar.

dialog_ratingbar.getRating()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top