ApplicationContext 在 onResume 中为 null

ApplicationContext is null in onResume

我今天早上醒来时发现了这个错误。我不确定它只发生在我片段的 onResume 中。因为它无法获取 ApplicationContext,所以我无法访问我的共享首选项。

这是我的简历代码

@Override
public void onResume() {
    super.onResume();

    if (mMediationAgent != null) {

        mMediationAgent.onResume(getActivity());

    } else {
        mMediationAgent = SupersonicFactory.getInstance();
        mMediationAgent.setRewardedVideoListener(this);
        mMediationAgent.initRewardedVideo(getActivity(), mAppKey, mUserId);
    }
    setupAd();
    SharedPreferences sharedPref = getActivity().getSharedPreferences("myPref", 0);
    int x = sharedPref.getInt("count", 0);
    Log.e("tree", "whats x" + Long.toString(x));
    if (count != 0) {
        ct.start();
        updateText(false);
    }
}

第一次初始化时请在onCreateView中添加这个

SharedPreferences sharedPref ;

sharedPref = getActivity().getSharedPreferences("myPref", 0);