Problem with fragment

1

I created a fragment and I'm trying to call it when I click on a button, but it gives an error and it appears in the console

  

com.example.gustavo.easypasse.RecargaActivity@30d409a0 must implement OnFragmentInteractionListener

And point to this line of code:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

How do I fix this?

    
asked by anonymous 08.11.2017 / 20:08

1 answer

2

Activity, to which this fragment is associated, must implement the OnFragmentInteractionListener interface.

That is, you have to declare Activity this way:

public class MainActivity extends AppCompatActivity implements OnFragmentInteractionListener {

    .....
    .....
}

and then implement their methods.

Normally the interface is declared in the Fragment and is used to communicate with the Activity, hence the obligation to implement it.

Some examples of using this approach:

08.11.2017 / 20:14