0

I have an activity with two fragments, Fragment A and Fragment B. I want to show a view hidden in Fragment A when the user touches a button in fragment B. How can I do this? I have tried to get the whole layout of the activity and get the view but I get a null pointer exception.

My activity layout is as shown below enter image description here This is the line I am using. It throws a null pointer exception.

shadowLine = getActivity().findViewById(R.id.shadowLine);
shadowLine.setVisibility(View.VISIBLE);
sammyukavi
  • 1,501
  • 2
  • 23
  • 51

1 Answers1

2

The simplest way, not the safest: You can access hosting activity in fragment B by

HostActivity activity =(HostActivity) getActivity();
activity.callOtherFragment();

In that activity, you can access fragment A, by

public void callOtherFragment() {
    YourFragment A = (YourFragment)getFragmentManager().findFragmentById(R.id.fragmentA);
    A.showSomeStuff();
}

then implement your method in fragment A:

public void showSomeStuff() {
  shadowLine.setVisibility(View.VISIBLE);
}
Maddy
  • 4,525
  • 4
  • 33
  • 53
burgyna
  • 149
  • 4