我有一个 Unity 游戏。我使用 3 个 RewardedAd(广告 ID 分开,以跟踪用户使用的是哪个奖励广告)。工作完美。 现在我想到了加载奖励视频的 UI 页面之一,我在同一个 UI 页面上添加了另一个 但是现在,当我打开奖励视频时,应用程序崩溃了。 我在应用程序启动时加载广告。
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
this.RewardedAd1 = RequestAndLoadRewardedAd(nameof(RewardedAdTypes.ad1));
this.RewardedAd2 = RequestAndLoadRewardedAd(nameof(RewardedAdTypes.ad2));
this.RewardedAd3 = RequestAndLoadRewardedAd(nameof(RewardedAdTypes.ad3));
this.RewardedAd4 = RequestAndLoadRewardedAd(nameof(RewardedAdTypes.ad4));
});
public RewardedAd RequestAndLoadRewardedAd(string place)
{
string adUnitId = "";
#if UNITY_ANDROID
if (place.Equals(nameof(RewardedAdTypes.ad1)))
{
adUnitId = Constants.DEBUG_ADS ? TEST_ANDROID_REWARDED_AD : ANDROID_REWARDED_AD_1;
}
else if (place.Equals(nameof(RewardedAdTypes.ad2)))
{
adUnitId = Constants.DEBUG_ADS ? TEST_ANDROID_REWARDED_AD : ANDROID_REWARDED_AD_2;
}
else if (place.Equals(nameof(RewardedAdTypes.ad3)))
{
adUnitId = Constants.DEBUG_ADS ? TEST_ANDROID_REWARDED_AD : ANDROID_REWARDED_AD_3;
}
else if (place.Equals(nameof(RewardedAdTypes.ad4)))
{
adUnitId = Constants.DEBUG_ADS ? TEST_ANDROID_REWARDED_AD : ANDROID_REWARDED_AD_4;
#else
adUnitId = "unexpected_platform";
#endif
// create new rewarded ad instance
RewardedAd rewardedAd = new RewardedAd(adUnitId);
if (place == nameof(RewardedAdTypes.ad1)) {
// Add Event Handlers
rewardedAd.OnAdLoaded += HandleRewardedAdLoaded_RewardedAd1;
rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad_RewardedAd1;
rewardedAd.OnAdOpening += HandleRewardedAdOpening_RewardedAd1;
rewardedAd.OnAdClosed += HandleRewardedAdClosed_RewardedAd1;
rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow_RewardedAd1;
rewardedAd.OnUserEarnedReward += HandleUserEarnedRewardedAd_RewardedAd1;
} else if(place == nameof(RewardedAdTypes.ad2)) {
// Add Event Handlers
rewardedAd.OnAdLoaded += HandleRewardedAdLoaded_RewardedAd2;
rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad_RewardedAd2;
rewardedAd.OnAdOpening += HandleRewardedAdOpening_RewardedAd2;
rewardedAd.OnAdClosed += HandleRewardedAdClosed_RewardedAd2;
rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow_RewardedAd2;
rewardedAd.OnUserEarnedReward += HandleUserEarnedRewardedAd_RewardedAd2;
} else if(place == nameof(RewardedAdTypes.ad3)) {
// Add Event Handlers
rewardedAd.OnAdLoaded += HandleRewardedAdLoaded_RewardedAd3;
rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad_RewardedAd3;
rewardedAd.OnAdOpening += HandleRewardedAdOpening_RewardedAd3;
rewardedAd.OnAdClosed += HandleRewardedAdClosed_RewardedAd3;
rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow_RewardedAd3;
rewardedAd.OnUserEarnedReward += HandleUserEarnedRewardedAd_RewardedAd3;
}else if(place == nameof(RewardedAdTypes.ad4)) {
// Add Event Handlers
rewardedAd.OnAdLoaded += HandleRewardedAdLoaded_RewardedAd4;
rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad_RewardedAd4;
rewardedAd.OnAdOpening += HandleRewardedAdOpening_RewardedAd4;
rewardedAd.OnAdClosed += HandleRewardedAdClosed_RewardedAd4;
rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow_RewardedAd4;
rewardedAd.OnUserEarnedReward += HandleUserEarnedRewardedAd_RewardedAd4;
}
rewardedAd.LoadAd(CreateAdRequest());
return rewardedAd;
}
public void HandleRewardedAdClosed_RewardedAd3(object sender, EventArgs args)
{
_onClosedAd_ad3?.Invoke();
if (DetachRewardedAdListeners(nameof(RewardedAdTypes.ad3))) {
this.RewardedAd3 = RequestAndLoadRewardedAd(nameof(RewardedAdTypes.ad3));
}
}
public void HandleUserEarnedRewardedAd_RewardedAd3(object sender, Reward args)
{
_onAd3_GotReward?.Invoke();
}
我对所有奖励广告都有相同的方法。
当我测试游戏时,我点击了奖励广告(ad3 或 ad4)。 我看到 AdOpening 事件运行。打开广告,然后我看到它火了
HandleUserEarnedRewardedAd_RewardedAd3 event received
AdManager:HandleUserEarnedRewardedAd_RewardedAd3(Object, Reward)
之后我得到一个
E/CRASH: Cause: null pointer dereference
在广告打开和奖励授予标题显示时。关闭广告后,应用程序会立即关闭。
有什么解决办法? 是否限制在一个应用中使用 4 个激励广告单元?或者我不必在请求新的激励广告之前分离 Ad3 变量?