0

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?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
chuckles
  • 71
  • 8
  • it might be possible that, id `switch1` is present but in another layout not the same layout where you are defining it. – bhuvnesh pattnaik Oct 01 '19 at 11:00
  • Please provide your Adapter code here :) – ravi Oct 01 '19 at 11:02
  • 'aSwitch' is null for some reason. It's probably because R.id.switch1 is not present in the layout you have inflated. – Isak Oct 01 '19 at 11:07
  • the button is not in that layout, this is the activity were I have the recyclerview, then I got the item_layout were I have the cardView with the Switch button. So, I need to implement a Switch button on the activity_layout even if I don't want to use it? – chuckles Oct 01 '19 at 11:42
  • Can you put full code of adaptor of recyclerview – ViduraPrasangana Oct 01 '19 at 12:12

2 Answers2

0
@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`
}
0

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.

A S M Sayem
  • 2,010
  • 2
  • 21
  • 28