我有一个alertdialog,显示评级栏,动态创建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-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" />
答案 0 :(得分:1)
您的评分对话框是对话框的一部分,因此您应使用dialog.findViewById
获取ratingBar
的值。
将相关行更改为以下内容:
...
dialog_ratingbar = (RatingBar) dialog.findViewById(R.id.dialog_ratingbar);
...
希望这能解决你的问题。
答案 1 :(得分:1)
首先改变它
dialog_ratingbar = (RatingBar) findViewById(R.id.dialog_ratingbar);
到
dialog_ratingbar = (RatingBar) dialog.findViewById(R.id.dialog_ratingbar);
// 并尝试按此按钮获取按钮上的评分栏值。
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();
}
});
答案 2 :(得分:1)
您正在获得评级栏,因为您正在使用活动类的findViewById
dialog_ratingbar = (RatingBar) dialog.findViewById(R.id.dialog_ratingbar);
同样,如果你想点击按钮上的评级值,那么你不必注册setOnRatingBarChangeListener,因为你可以直接从评级栏中获取价值。
dialog_ratingbar.getRating()