android - 无法从警报对话框和异常错误中的 RatingBar 获取值 (Android)

标签 android ratingbar

我有一个显示评级栏的警报对话框,动态创建一个评级栏并将其分配给警报对话框。 .当我触发 onclick 监听器时,它显示 NullPointerException。 如何在警报对话框的 OK 处理程序中获取评级值? 当我这样做时,我得到一个空指针异常警告。因此,问题是如何获取评分值?

谢谢。

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

XML 布局

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

最佳答案

首先改变它

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

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

//然后像这样尝试获取 Rating Bar 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(); 
                    }                   
                });

关于android - 无法从警报对话框和异常错误中的 RatingBar 获取值 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23576645/

相关文章:

java - Android中Content Provider的Uri中的 "content://"可以替换吗?

android - 单击菜单时如何隐藏默认键盘?

android - 评级栏不工作

android - 如何在 ListView 中设置 RatingBar 的样式? (API 21 和低版本中的问题)

java - 无法为RatingBar设置评级

Android Rating Bar 只显示满星,不显示半星

android - 如何用两个手指滚动位图?

android - 如何使用 achartengine 在 android 中创建条形图

android - 迁移到 Androidx 后设计 View 和导入问题

android - 具有各种屏幕尺寸的自定义评分栏