MyUnifiedNativeAd.OnUnifiedNativeAdLoaded(Android.Gms.Ads.Formats.UnifiedNativeAd 广告)在某些设备上崩溃 _ Xamarin.Android
MyUnifiedNativeAd.OnUnifiedNativeAdLoaded (Android.Gms.Ads.Formats.UnifiedNativeAd ad) crash on some devices _ Xamarin.Android
我的 Xamarin.Android 应用程序在某些设备上崩溃
Stacktrace 说 MyUnifiedNativeAd.OnUnifiedNativeAdLoaded (Android.Gms.Ads.Formats.UnifiedNativeAd 广告)
这是堆栈跟踪
MyUnifiedNativeAd.OnUnifiedNativeAdLoaded (Android.Gms.Ads.Formats.UnifiedNativeAd ad)
UnifiedNativeAd+IOnUnifiedNativeAdLoadedListenerInvoker.n_OnUnifiedNativeAdLoaded_Lcom_google_android_gms_ads_formats_UnifiedNativeAd_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_ad)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.26(intptr,intptr,intptr)
这是我的代码,我在其中膨胀和初始化 Listener
UnifiedNativeAdView adView = (UnifiedNativeAdView)LayoutInflater.Inflate(Resource.Layout.ad_unified, null);
FrameLayout frameLayout = inflated.FindViewById<FrameLayout>(Resource.Id.frameLayout1);
var a = new MyUnifiedNativeAd(adView, frameLayout);
AdLoader adLoader = new AdLoader.Builder(Context, Resources.GetString(Resource.String.ad_unit_id))
.ForUnifiedNativeAd(a)
.WithAdListener(new MyAdListener())
.Build();
adLoader.LoadAd(new AdRequest.Builder().Build());
这是实现 IOnUnifiedNativeAdLoadedListener 接口的监听器Class
class MyUnifiedNativeAd : Java.Lang.Object, IOnUnifiedNativeAdLoadedListener
{
UnifiedNativeAdView AdView = null;
FrameLayout frame = null;
public MyUnifiedNativeAd( UnifiedNativeAdView adv, FrameLayout f)
{
AdView = adv;
frame = f;
}
public void OnUnifiedNativeAdLoaded(UnifiedNativeAd ad)
{
if (ad is null)
return;
MediaView mv = AdView.FindViewById<MediaView>(Resource.Id.ad_media);
TextView advertiser = AdView.FindViewById<TextView>(Resource.Id.ad_advertiser);
TextView headline = AdView.FindViewById<TextView>(Resource.Id.ad_headline);
ImageView img = AdView.FindViewById<ImageView>(Resource.Id.ad_app_icon);
TextView body = AdView.FindViewById<TextView>(Resource.Id.ad_body);
RatingBar rating = AdView.FindViewById<RatingBar>(Resource.Id.ad_stars);
TextView price = AdView.FindViewById<TextView>(Resource.Id.ad_price);
TextView store = AdView.FindViewById<TextView>(Resource.Id.ad_store);
Button action = AdView.FindViewById<Button>(Resource.Id.ad_call_to_action);
List<View> v = new List<View>() { mv, advertiser, headline, img, body, rating, price, store, action };
action.Text = ad.CallToAction;
store.Text = ad.Store;
price.Text = ad.Price;
rating.NumStars = ad.StarRating is null ? 0 : ad.StarRating.IntValue();
body.Text = ad.Body;
img.SetImageDrawable(ad.Icon.Drawable);
headline.Text = ad.Headline;
advertiser.Text = ad.Advertiser;
AdView.MediaView = mv;
AdView.SetNativeAd(ad);
frame.RemoveAllViews();
frame.AddView(AdView);
}
}
我解决了我的问题
我认同
在 "string.xml" 文件中添加 "google_app_id" 后,admob app id 值,我测试了很多次,错误消失了
已解决!
有时 Google 向我们发送没有图标的广告。
如果广告没有图标,则表示图标 属性 为空。
当我尝试将 Icon 设置为 ImageView 时,它抛出异常
我的 Xamarin.Android 应用程序在某些设备上崩溃 Stacktrace 说 MyUnifiedNativeAd.OnUnifiedNativeAdLoaded (Android.Gms.Ads.Formats.UnifiedNativeAd 广告)
这是堆栈跟踪
MyUnifiedNativeAd.OnUnifiedNativeAdLoaded (Android.Gms.Ads.Formats.UnifiedNativeAd ad)
UnifiedNativeAd+IOnUnifiedNativeAdLoadedListenerInvoker.n_OnUnifiedNativeAdLoaded_Lcom_google_android_gms_ads_formats_UnifiedNativeAd_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_ad)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.26(intptr,intptr,intptr)
这是我的代码,我在其中膨胀和初始化 Listener
UnifiedNativeAdView adView = (UnifiedNativeAdView)LayoutInflater.Inflate(Resource.Layout.ad_unified, null);
FrameLayout frameLayout = inflated.FindViewById<FrameLayout>(Resource.Id.frameLayout1);
var a = new MyUnifiedNativeAd(adView, frameLayout);
AdLoader adLoader = new AdLoader.Builder(Context, Resources.GetString(Resource.String.ad_unit_id))
.ForUnifiedNativeAd(a)
.WithAdListener(new MyAdListener())
.Build();
adLoader.LoadAd(new AdRequest.Builder().Build());
这是实现 IOnUnifiedNativeAdLoadedListener 接口的监听器Class
class MyUnifiedNativeAd : Java.Lang.Object, IOnUnifiedNativeAdLoadedListener
{
UnifiedNativeAdView AdView = null;
FrameLayout frame = null;
public MyUnifiedNativeAd( UnifiedNativeAdView adv, FrameLayout f)
{
AdView = adv;
frame = f;
}
public void OnUnifiedNativeAdLoaded(UnifiedNativeAd ad)
{
if (ad is null)
return;
MediaView mv = AdView.FindViewById<MediaView>(Resource.Id.ad_media);
TextView advertiser = AdView.FindViewById<TextView>(Resource.Id.ad_advertiser);
TextView headline = AdView.FindViewById<TextView>(Resource.Id.ad_headline);
ImageView img = AdView.FindViewById<ImageView>(Resource.Id.ad_app_icon);
TextView body = AdView.FindViewById<TextView>(Resource.Id.ad_body);
RatingBar rating = AdView.FindViewById<RatingBar>(Resource.Id.ad_stars);
TextView price = AdView.FindViewById<TextView>(Resource.Id.ad_price);
TextView store = AdView.FindViewById<TextView>(Resource.Id.ad_store);
Button action = AdView.FindViewById<Button>(Resource.Id.ad_call_to_action);
List<View> v = new List<View>() { mv, advertiser, headline, img, body, rating, price, store, action };
action.Text = ad.CallToAction;
store.Text = ad.Store;
price.Text = ad.Price;
rating.NumStars = ad.StarRating is null ? 0 : ad.StarRating.IntValue();
body.Text = ad.Body;
img.SetImageDrawable(ad.Icon.Drawable);
headline.Text = ad.Headline;
advertiser.Text = ad.Advertiser;
AdView.MediaView = mv;
AdView.SetNativeAd(ad);
frame.RemoveAllViews();
frame.AddView(AdView);
}
}
我解决了我的问题 我认同 在 "string.xml" 文件中添加 "google_app_id" 后,admob app id 值,我测试了很多次,错误消失了
已解决! 有时 Google 向我们发送没有图标的广告。 如果广告没有图标,则表示图标 属性 为空。 当我尝试将 Icon 设置为 ImageView 时,它抛出异常