MoPub横幅与全屏广告调用报错求助:刷新禁用及IntentReceiver泄漏
MoPub广告集成报错解决方案:刷新禁用+Receiver泄漏问题
Hey there, let's work through these two MoPub ad integration bugs you're facing—they're pretty common, so we can fix them step by step.
报错1:广告单元刷新已禁用(单元ID见此处)
可能原因
- 你的MoPub后台广告单元配置中,自动刷新功能被关闭了
- 代码中手动调用了
setAutorefreshEnabled(false)禁用了刷新 - 广告单元ID填写错误,导致请求的单元不存在或被平台限制
解决方案
- 检查MoPub后台设置:登录MoPub平台,找到对应的广告单元,确认「Auto-refresh」选项是开启状态(通常默认开启,但可能被误操作关闭)
- 排查代码逻辑:搜索项目中是否有类似
mMoPubView.setAutorefreshEnabled(false)的代码,如果不需要禁用刷新,直接注释掉这行;如果是手动刷新场景,确保在合适的业务时机调用mMoPubView.refresh()触发刷新 - 验证单元ID:确认代码中使用的单元ID和后台创建的完全一致,没有拼写错误或复制遗漏
报错2:Activity已泄漏最初在此注册的IntentReceiver com.mopub.mobileads.MoPubView$1@a72e13a,是否遗漏unregisterReceiver()调用?
可能原因
这个问题是因为MoPub的MoPubView或MoPubInterstitial内部注册的广播接收器,在Activity销毁时没有被正确注销,导致内存泄漏。核心原因是你没有在Activity的生命周期方法中正确释放广告资源。
解决方案
- 处理横幅广告(MoPubView):在Activity的
onDestroy()方法中,调用mMoPubView.destroy()方法,这会自动注销内部的广播接收器并释放关联资源:@Override protected void onDestroy() { super.onDestroy(); if (mMoPubView != null) { mMoPubView.destroy(); } } - 处理插屏广告(MoPubInterstitial):针对你代码中初始化的插屏广告
mInterstitial_recent,同样要在Activity销毁前清理资源:
额外建议:在插屏广告展示完成后(比如@Override protected void onDestroy() { super.onDestroy(); if (mInterstitial_recent != null) { mInterstitial_recent.destroy(); } }onInterstitialDismissed()回调中),也可以调用destroy()或clear(),提前释放闲置资源
针对你的插屏代码补充
你当前的插屏初始化代码:
mInterstitial_recent = new MoPubInterstitial(MainActivity.this, getString(R.string.Recent_Matches_Interstitial)); mInterstitial_recent.load...
要确保MainActivity的上下文没有被广告实例长期持有,避免不必要的内存引用。
内容的提问来源于stack exchange,提问作者Pranav Fulkari




