为什么每次触发按钮都会使屏幕变白。但这些价值观正在被去除。这是我为它抛出错误的地方编写的代码。
final String uniqueKey = requestFormArrayList.get(position).getRequestid();
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("ResearchRequest").child(uniqueKey);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String termsAgreement = snapshot.child("requestStatus").getValue(String.class);
if(termsAgreement.equals("accepted")){
holder.acceptresearch_request.setText("accepted");
holder.acceptresearch_request.setEnabled(false);
holder.deleteresearch_request.setEnabled(false);
holder.publishresearch_request.setEnabled(true);
}
else if (termsAgreement.equals("published")){
holder.acceptresearch_request.setEnabled(false);
holder.deleteresearch_request.setEnabled(false);
holder.publishresearch_request.setText("published");
holder.publishresearch_request.setEnabled(false);
}
else{
holder.acceptresearch_request.setEnabled(true);
holder.deleteresearch_request.setEnabled(true);
holder.publishresearch_request.setEnabled(false);
}
}
上面的代码使按钮的启用/禁用取决于条件的值。但每次我删除或触发删除按钮时,它都会将屏幕变白并返回MainActivity,但它会毫无问题地删除数据,只是它会变白而不是烤面包,这是我的代码。
holder.deleteresearch_request.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Delete Research Record");
alert.setMessage("Are you sure you want to delete");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String uniqueKey = requestFormArrayList.get(position).getRequestid();
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("ResearchRequest").child(uniqueKey);
databaseReference.removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Log.i("firebase", "Throwing exception");
throw new RuntimeException(task.getException());
}
else {
Toast.makeText(context, "Research has been deleted", Toast.LENGTH_SHORT).show();
}
}
});
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "Cancelled", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
alert.show();
}
});
编辑:这就是我遇到的错误,我在这里搜索了NullPointerException。但我不太了解
2021-01-07 16:37:28.706 25163-25163/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.citeresearchrepository, PID: 25163
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.citeresearchrepository.AdminAdapter.AdminRequestFormAdapter$1.onDataChange(AdminRequestFormAdapter.java:60)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8347)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
我应该在termsAgreement.isEmpty((中添加另一个条件吗?我认为它是在上面的条件下,因为当你删除它时,没有术语Agreement.equals("已删除"(;在数据库中,有没有这个解决方案的想法?
String termsAgreement = snapshot.child("requestStatus").getValue(String.class);
if(termsAgreement.equals("accepted")){
holder.acceptresearch_request.setText("accepted");
holder.acceptresearch_request.setEnabled(false);
holder.deleteresearch_request.setEnabled(false);
holder.publishresearch_request.setEnabled(true);
}
else if (termsAgreement.equals("published")){
holder.acceptresearch_request.setEnabled(false);
holder.deleteresearch_request.setEnabled(false);
holder.publishresearch_request.setText("published");
holder.publishresearch_request.setEnabled(false);
}
else{
holder.acceptresearch_request.setEnabled(true);
holder.deleteresearch_request.setEnabled(true);
holder.publishresearch_request.setEnabled(false);
}
此处,您的termsAgreement
为空。看看你写的String.class
的最上面一行。字符串是数据类型。那么,你怎么称呼它呢?我想这就是你犯错的原因。也许,你真的想写string.class
。在.getValue(String.class)
上,您无法调用value。这就是你犯错的原因。