Cannot resolve method getActivity()

我只是一个虾纸丫 提交于 2019-11-30 17:12:12
zohreh

You can use:

this Or `MainActivity.this`

Instead of:

getActivity()

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.

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);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!