Я новичок в Android и пытаюсь передать рейтинг в диалоге, но он бросает NPE.
Это код моей текущей деятельности.…
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");
}
}
});
У меня есть метод для добавления рейтинга в том же классе
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
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$2.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