将评级栏传递给对话框

Pass rating bar to dialog

我是 android 的新手,正在尝试将评级栏传递给对话框,但它引发了 NPE。

这是我的 oncreate activity 上的代码。 …

LinearLayout rating = findViewById(R.id.rating);
ratingBar = findViewById(R.id.ratingsbar);
        flag = true;
        rating.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag) {
                    RatingBarDialog ratingBarDialog = new RatingBarDialog();
                    Bundle args = new Bundle();
                    args.putString("profileUid", profileUid);
                    ratingBarDialog.setArguments(args);
                    ratingBarDialog.show(getFragmentManager(), "rating");
                }

            }
        });

我有在同一个 class

中添加评分的方法
 static public void addRatingToUser(float newRating, String profileUid) {

    // calculate new rating within User class
        dbUser.addRating((double) newRating);

        // add new rating and ratingCount to firebase
            DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("users").child(profileUid);
            userRef.child("rating").setValue(dbUser.getRating());
            userRef.child("ratingCount").setValue(dbUser.getRatingCount());

            // set the stars of the ratingBar view
            ratingBar.setRating((float) dbUser.getRating());
        }
    }

这是我的 RatinDialog class

public class RatingBarDialog extends DialogFragment {
  @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    final LayoutInflater inflater = LayoutInflater.from(builder.getContext());

    @SuppressLint("InflateParams") final View view = inflater.inflate(R.layout.activity_rate_user, null);
    final RatingBar ratingBar = view.findViewById(R.id.editRatingBar);

    final String profileUid = getArguments().getString("profileUid");

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(view)
            .setTitle("Rate User")
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                    UserProfile.addRatingToUser(ratingBar.getRating(), profileUid);

                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    RatingBarDialog.this.getDialog().cancel();
                }
            });
    return builder.create();
}

}

我有一个具有计算评分方法的模型用户

void addRating(double newRating) {
    // get total of all old ratings (multiply current average rating by # of ratings
    double oldRatingTotal = rating * ratingCount;

    // increment rating count
    ratingCount++;

    // add new rating to the total, then divide it by the new rating count to get new average
    double newRatingTotal = oldRatingTotal + newRating;
    rating = newRatingTotal / ratingCount;
}

这是我的 logcat 错误,我已经尝试但无法修复 NPE。我将不胜感激任何帮助。

谢谢

2018-12-18 19:49:56.710 27009-27009/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.android.reseller, PID: 27009
    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.android.reseller.User.addRating(double)' on a null object reference
        at com.android.reseller.UserProfile.addRatingToUser(UserProfile.java:215)
        at com.android.reseller.RatingBarDialog.onClick(RatingBarDialog.java:38)
        at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6566)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$Metho

最后,我能够修复错误。 我最初创建了一个模型变量:

静态用户 dbUser;

但是在我的oncreate方法中创建模型对象失败,导致dbUser为null。

dbUser = new User();