Problem with Fragment

Asked

Viewed 63 times

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.ample.Gustavo.easypasse.Reloade@30d409a0 must implement Onfragmentinteractionlistener

And point to that 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 correct that?

1 answer

2

Activity, to which this Fragment is associated, must implement the interface Onfragmentinteractionlistener.

That is, you must declare Activity in this way:

public class MainActivity extends AppCompatActivity implements OnFragmentInteractionListener {

    .....
    .....
}

and then implement their methods.

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

Some examples of using this approach:

  • And how should I implement? kkkk

  • If you who created Fragment don’t know, how can I know if I don’t know his code?

  • First time working with Fragment kkkk I don’t know how to implement.

  • It was you who wrote the code of this Fragment?

  • In fact I only created and assembled the screens of this Ragment, the Android Studio who assembled her Activity.

  • 1

    Ok. If you do not need to communicate with Activity delete the method onAttach(), otherwise see the examples I added to the reply. Remember that I don’t have the ability to guess what you intend to do :)

  • Before anything @Gustavosevero, recommend reading of this article, from Android Studio’s own doc, instead of using the DLSF technique (Drive Crazy and Go Doing)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.