Androidn error: content.res.Resources android.content.Context.getResources()' on a null object

The error position is that you need to pass the activity when getting the screen width and height in the Fragment.
GetActivity() is used, but this error will be reported in a few cases.

This is the wrong code:

  //step3:创建广告请求参数AdSlot,具体参数含义参考文档
        float expressViewWidth = UIUtils.getScreenWidthDp(getActivity());
        float expressViewHeight = UIUtils.getHeight(getActivity());

The steps to start the correction, the code: The
reason for the error is that the getActivity() cannot be found can not find the Context The
solution is to add a global variable to the Activity,

public static IndexPageActivity application;

Then declare in onCreate()

application = this;

Finally, directly refer to the global environment pointed to in the Activity

//step3:创建广告请求参数AdSlot,具体参数含义参考文档
        float expressViewWidth = UIUtils.getScreenWidthDp(IndexPageActivity.application);
        float expressViewHeight = UIUtils.getHeight(IndexPageActivity.application);

Guess you like

Origin blog.csdn.net/ShiXinXin_Harbour/article/details/112467107