Android Studio getting “Must implement OnFragmentInteractionListener”

前端 未结 3 1745
天涯浪人
天涯浪人 2021-02-19 10:52

I\'m getting a throw that says \"Must implement OnFragmentInteractionListener, and I already have it...

I\'ve looked at every question asked here, and no one helped me.<

相关标签:
3条回答
  • 2021-02-19 11:10

    Here is why the above solutions eliminate the error. The if statement inside the overriden onAttach method of FragmentCamera.java is checking if the fragment's parent activity implements the OnFragmentInteractionListener interface, which is defined inside FragmentCamera.java. The corresponding else statement throws an error if the parent activity does not implement the interface. The interface is useful but not necessary.

    By the way, onAttach is the fragment's first lifecycle method, and it is "called once the fragment is associated with its activity". Its deprecated version is: onAttach(Activity). Its new version is: onAttach(Context). Hope this helps someone.

    0 讨论(0)
  • 2021-02-19 11:20

    Just remove this line:

    throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
    

    There is no need to remove the entire method.

    0 讨论(0)
  • 2021-02-19 11:29

    Remove onAttach method from your Fragment, then it'll be okay.

    0 讨论(0)
提交回复
热议问题