I'm kinda new to Android developing and I hope you can help me. I have one RecyclerView, with each cardview showing one image, two text views and one switch button. This app is for myself and I wanted to organize my card collection so the switch is there for letting me know if I have a card or not, so I wanted to update database with a 1, if it's checked, or with a 0 if it's not. I was trying to test the buttons with some Toast but I get an error. Here the code:
aSwitch = (Switch) findViewById(R.id.switch1);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
Toast.makeText(StockActivity.this, "On", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(StockActivity.this, "Off", Toast.LENGTH_SHORT).show();
}
}
});
This is my code for the switch button, I thought this was fine but I got an error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.battlespiritsdb/com.example.battlespiritsdb.StockActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
The error points at first line of setOnCheckedChangeListener, and I don't know why, is it because I'm using them on RecyclerView or something?