初始化
Switch notifswitch;
Switch notifswitch = (Switch) findViewById(R.id.simpleSwitch);
Firebase参考
myrefNotif = FirebaseDatabase.getInstance().
getReference("Questions").child(questionId).child("Notif");
代码:
myrefNotif.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
notifswitch.setChecked(true); // << ERROR ORIGIN
}
else {
notifswitch.setChecked(false);
}
}
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Switch.setChecked(boolean)' (目录)= >>>> QuestionProfile $ 3.onDataChange