'Problem with Switch buttons on RecyclerView

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?



Solution 1:[1]

@Kinda --Try This code
sw1 = (Switch)findViewById(R.id.switch1);
        sw2 = (Switch)findViewById(R.id.switch2);
        btnGet = (Button)findViewById(R.id.getBtn);
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1, str2;
                if (sw1.isChecked())
                    str1 = sw1.getTextOn().toString();
                else
                    str1 = sw1.getTextOff().toString();
                if (sw2.isChecked())
                    str2 = sw2.getTextOn().toString();
                else
                    str2 = sw2.getTextOff().toString();
                Toast.makeText(getApplicationContext(), "Switch1 -  " + str1 + " \n" + "Switch2 - " + str2,Toast.LENGTH_SHORT).show();
            }
        });
    }`enter code here`
}

Solution 2:[2]

You can try this snippet of code.

switchButton = (Switch) findViewById(R.id.switch1);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {

            if (isChecked) {
                 // if 'isChecked' is true do whatever you need...
                 // To show a toast use only 'this' (for activity) or 'getActivity()'
                 // (for fragment) nothing else.
                 Toast.makeText(this, "On", Toast.LENGTH_SHORT).show(); // or getActvity()
            } else {
                 Toast.makeText(this, "Off", Toast.LENGTH_SHORT).show(); // or getActvity()
            }
        }
    }
});

Hope it will help.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sujeet Kumar
Solution 2 A S M Sayem