我想比较用户点击购买东西时数据库中的数据,数据会与数据库进行比较。如果用户患有此病,则不允许用户购买。任何人都可以检查我的代码是否有任何错误?
private void submit(){
myRef.child("Users").child(userID).child("medical");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
if (postSnapshot.child("medical").getValue(String.class) .equals("Diabetes") ) {
Toast.makeText(getApplicationContext(), "Diabetes", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), Medicine.class));
}else{
}
}
}
java.lang.NullPointerException:尝试调用虚方法 ' boolean java.lang.String.equals(java.lang.Object)'在null对象上 参考 在 com.bleach.finalyearproject.Pain_and_Fever $ 1.onDataChange(Pain_and_Fever.java:61
这是用户密钥的页面,其信息和存储在数据库中
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: Submit pressed.");
String name = mName.getText().toString();
String address = mAddress.getText().toString();
String phoneNum = mPhoneNum.getText().toString();
String gender = mGender.getText().toString();
String country = mCountry.getText().toString();
String medicine = mMedicine.getText().toString();
String medical = rb.getText().toString();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
Log.d(TAG, "onClick: Attempting to submit to database: \n" +
"name: " + name + "\n" +
"address: " + address + "\n" +
"phone number: " + phoneNum + "\n" +
"gender: " + gender + "\n" +
"country: " + country + "\n" +
"medicine: " + medicine + "\n" +
"medical:" + medical + "\n"
);
//handle the exception if the EditText fields are null
if(!name.equals("") && !address.equals("") && !phoneNum.equals("") && !gender.equals("") && !country.equals("") && !medicine.equals("")){
UserInformation userInformation = new UserInformation(name,address,phoneNum,gender,country,medicine,medical);
myRef.child("Users").child(userID).setValue(userInformation);
toastMessage("Information has been saved.");
mName.setText("");
mAddress.setText("");
mPhoneNum.setText("");
mGender.setText("");
mCountry.setText("");
mMedicine.setText("");
rb.setText("");
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
}else{
toastMessage("Fill out all the fields");
startActivity(new Intent(getApplicationContext(),AddToDatabase.class));
}
}
});
}
public void rbclick(View view){
int radiobuttonid = rg.getCheckedRadioButtonId();
rb = (RadioButton) findViewById(radiobuttonid);
Toast.makeText(getApplicationContext(),rb.getText(),Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:0)
如果用户有糖尿病,您可以使用查询加载数据:
myRef.child("Users").child(userID)
.orderByChild("medical").equalTo("Diabetes").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Toast.makeText(getApplicationContext(), "Diabetes", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), Medicine.class));
}
}
或者您可以加载用户并在客户端代码中进行检查:
myRef.child("Users").child(userID)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("medical").getValue(String.class).equals("Diabetes")) {
Toast.makeText(getApplicationContext(), "Diabetes", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), Medicine.class));
}
}