0

I have a navigation drawer Android project that has several fragments. Inside one of the fragment layouts, I have a toggle switch. I have this simple handler stub inside of the fragment activity:

public void onCellDataSettingClicked(View view) {
        boolean on = ((Switch) view).isChecked();

        if (on) {
            // Enable vibrate
        } else {
            // Disable vibrate
        }
    }

The problem is that when I get to this line "boolean on = ((Switch) view).isChecked();" the program crashes. I am unable to access the Switch from my fragment activity. If I place the same code inside of my main activity, everything works.

How do I get my handler to work inside of the associated fragment's activity and not just the main activity?

kinezu
  • 1,212
  • 2
  • 12
  • 23

1 Answers1

0

If I'm following you, you just need to make a getter in your parent activity for the boolean, then call getActivity().isMyBooleanName() from your fragment. See this answer.

Community
  • 1
  • 1
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19