我是android的新手,正尝试将等级栏传递给对话框,但会引发NPE。
这是我oncreate活动上的代码。 …
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
答案 0 :(得分:0)
最后,我能够解决该错误。 我最初创建了模型的变量:
静态用户dbUser;
但是无法在我的oncreate方法中创建模型的对象,这导致dbUser为空。
dbUser = new User();