1

i have an activity with 4 fragments from fragment number 1 I want to enable an existing button (that is disable) on fragment 3, when i click in my button in fragment1. this is my attempt: fragment 1:

public class FragmentEvolucion  extends Fragment {
//btnGuardar is in fragment1, the others are in fragment 3 and 4
 Button btnGuardar, btnHabilitarMed, btnHabilitarImc;

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_evolucion, container, false);
    btnGuardar=(Button)rootView.findViewById(R.id.btnGuardarEvolucion);
    btnHabilitarMed=(Button)rootView.findViewById(R.id.btnGuardarMedicacion);
    btnHabilitarImc=(Button)rootView.findViewById(R.id.btnGuardarDiagnostico);

   btnGuardar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btnHabilitarMed.setEnabled(true);
            btnHabilitarImc.setEnabled(true);
  }
    });

this give me an error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference

How can i access the button and change it status enabled correctly?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
matQ
  • 597
  • 2
  • 13
  • 27
  • 1
    [Communicating with Other Fragments](https://developer.android.com/training/basics/fragments/communicating.html) – matoni Jul 29 '17 at 17:06
  • your button in fragment or activity – Anil Jul 29 '17 at 17:07
  • `fragment_evolucion.xml` apparently doesn't have `btnGuardarMedicacion` or `btnGuardarDiagnostico`. You are only finding from the current Fragment, so where is the reference to the other one? – OneCricketeer Jul 29 '17 at 17:08
  • Along with first comment. https://stackoverflow.com/questions/24777985/how-to-implement-onfragmentinteractionlistener – OneCricketeer Jul 29 '17 at 17:10
  • @cricket_007 and how can i reference to the other fragment from fragmentEvolucion? thanks – matQ Jul 29 '17 at 17:11
  • Think you should either 1) Keep the logic in Activity - that is, have e.g. frag 3 call `((MyActivity)getActivity()).enableButtonInFrag4()` and have the Activity call a method on frag 4 to enable the button, hope you get the picture. Otherwise 2) Use EventBus or the like to broadcast events that should enable/disable buttons. This has the advantage of not requiring your Activity to know about the inner workings of your fragments, but can be a bit harder to maintain in the long run. – cYrixmorten Jul 29 '17 at 17:13
  • @cYrixmorten and in method enableButtonInFrag4() how can i reference to the button in frag4? fragment 4 xml is fragment_medicacion.xml – matQ Jul 29 '17 at 17:25
  • 1
    try to use interfaces – Quick learner Jul 29 '17 at 17:33
  • You don't exactly "reference" anything except from the class itself. You pass "events" between the Fragments, through the Activity, using interfaces, or an EventBus – OneCricketeer Jul 29 '17 at 17:38
  • Using interfaces is the best option for your problem. – Prashant Kumar Sharma Jul 29 '17 at 17:53
  • @PrashantSharma can you show an example with more details, please? – matQ Jul 29 '17 at 17:56

1 Answers1

1

First of all, create a interface.

UpdateButtonListener.java

public interface UpdateButtonListener {
    void onUpdate(boolean status);
}

Now in fragment 3, implement interface to class

public class Fragment3 extends Fragment implements UpdateButtonListener{

public static UpdateButtonListener updateButton;

@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment3, container, false);
        updateButton = this;
        return view;
         }

    @Override
    public void onUpdate(boolean status) {
        // here set the functionality of button whether to disable or not .
        if(status){
        btnHabilitarMed.setEnabled(true);
        btnHabilitarImc.setEnabled(true);
        }
    }
 }

Now in first fragment.

btnGuardar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Fragment3.updateButton.onUpdate(true);
  }

Like-wise do for other's.

Prashant Kumar Sharma
  • 1,120
  • 1
  • 11
  • 21