Cannot resolve method getActivity()

后端 未结 3 2067
猫巷女王i
猫巷女王i 2020-12-16 10:58
  • I am new to Android and learning to create fragments in Android by following this
    example: Fragment Navigation Drawer

  • The code between N

相关标签:
3条回答
  • 2020-12-16 11:03

    In Fragment it is best to use onAttach() method to get the instance of an Activity attached to it.

    @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
        }
    
    0 讨论(0)
  • 2020-12-16 11:06

    An Activity has no getActivity() method.
    Fragments have.

    Because getActivity() says: "return the Activity which contains me".

    And while Framents are contained in Activities, Activities themselves aren't.

    0 讨论(0)
  • 2020-12-16 11:10

    You can use:

    this Or `MainActivity.this`
    

    Instead of:

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