检查switch ui小部件的值(如果已打开)

时间:2018-08-22 10:19:28

标签: android firebase firebase-realtime-database uiswitch

初始化

    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

0 个答案:

没有答案