在 activity 中设置 google 分析跟踪器时,未定义类型 activity 的方法 getActivity()

when setting a google analytics tacker in an activity, method getActivity() is undefined for the type activity

我想为我的应用程序设置分析,它显示方法 getActivity() 未定义 MainScreen 类型。我已经设置了“MyAplication”,我没有收到任何错误,所以这是主屏幕代码:

@Override
    public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

    Tracker t = ( (MyApplication) getActivity().getApplication()).getTracker(
                      TrackerName.APP_TRACKER);

                  // Set screen name.
                  t.setScreenName("MainScreen");

                  // Send a screen view.
                  t.send(new HitBuilders.ScreenViewBuilder().build());



    } 

Activity class doesn't have getActivity() method. If you need to access the activity use this. instead (or just call the activity method directly). You probably copied the code from a Fragment example. Fragments are not activities and you need to get access to the activity with getAcitivty()方法