我正在尝试从Firebase删除子字符串,但是我不断收到错误Attempting to invoke method on a null object reference
。删除子项的代码行是:
DatabaseLike.child(uploadCurrent.getName()).child(mAuth.getCurrentUser().getUid()).removeValue();
完整的方法是这样的:
public void onBindViewHolder(final ImageViewHolder holder, final int
position) {
//database reference
mDatabaseRef = FirebaseDatabase.getInstance().getReference("likes");
//mCurrentUserId = mAuth.getCurrentUser().getUid();
mAuth = FirebaseAuth.getInstance();
final UploadClass uploadCurrent = mUploads.get(position);
//new code 10/1/2019
//final String post_key = mUploads.get(position).toString();
//end
holder.textViewName.setText(uploadCurrent.getName());
Picasso.with(mContext)
.load(uploadCurrent.getImageUrl())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(holder.imageView);
holder.imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(mContext, uploadCurrent.getName(),
Toast.LENGTH_LONG).show();
}
});
holder.mLikebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mProcessLike = true;
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if (mProcessLike) {
if (dataSnapshot.child(uploadCurrent.getName()).hasChild(mAuth.getCurrentUser().getUid())) {
//Cant use unlike as it is string value
mDatabaseLike.child(uploadCurrent.getName()).child(mAuth.getCurrentUser().getUid()).removeValue();
mProcessLike = false;
} else {
mDatabaseRef.child(uploadCurrent.getName()).child(mAuth.getCurrentUser().getUid()).setValue("Like");
mProcessLike = false;
}
}
}
@Override
@Override
public void onCancelled(@NonNull DatabaseError
databaseError) {
}
});