Android:将评级栏值传递给对话框

时间:2014-08-29 14:31:07

标签: android dialog fragment ratingbar

我的目标是在点击评级栏后,此评级栏的值将传递给显示新评级栏的DialogFragment。

这是我的活动代码(onCreate)

    ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            if (fromUser){
            RateItemDialogFragment newFragment = new RateItemDialogFragment();

            Bundle bundle = new Bundle();
            bundle.putFloat("ratingValue", rating);
            newFragment.setArguments(bundle);
            newFragment.show(getSupportFragmentManager(), "rate menu item");

            }
        }
    });

这是我的DialogFragment

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();


    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_rate_item, null))

            .setMessage(R.string.title_rating_dialog)
            .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Send the positive button event back to the host activity
                    mListener.onDialogPositiveClick(RateItemDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Send the negative button event back to the host activity
                    mListener.onDialogNegativeClick(RateItemDialogFragment.this);
                    // User cancelled the dialog
                    RateItemDialogFragment.this.getDialog().cancel();
                }
            })
    ;

    Dialog dialog = builder.create();

    // set the ratingBar to the right value
    Bundle bundle = getArguments();
    ((RatingBar) dialog.findViewById(R.id.rating_bar_dialog)).setRating(bundle.getFloat("ratingValue"));

    // Create the AlertDialog object and return it
    return dialog;
}

这给了我一个

的JavaNullPointerException
        ((RatingBar) dialog.findViewById(R.id.rating_bar_dialog)).setRating(bundle.getFloat("ratingValue"));

我必须在Fragment生命周期中做错事,但我无法弄明白......谢谢!

1 个答案:

答案 0 :(得分:0)

您的观点可能不是对话框的孩子。

试试这个,在膨胀的对话框视图中查找您的视图。     查看root = inflater.inflate(R.layout.dialog_rate_item,null);     builder.setView(根)

            .setMessage(R.string.title_rating_dialog)
            .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Send the positive button event back to the host activity
                    mListener.onDialogPositiveClick(RateItemDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Send the negative button event back to the host activity
                    mListener.onDialogNegativeClick(RateItemDialogFragment.this);
                    // User cancelled the dialog
                    RateItemDialogFragment.this.getDialog().cancel();
                }
            });

然后

    ((RatingBar) root.findViewById(R.id.rating_bar_dialog)).setRating(bundle.getFloat("ratingValue"));