flutter - Rewarded Video Ads Error when Reload : “ad_not_loaded, show failed for rewarded video, no ad was loaded, null)”

老子叫甜甜 提交于 2019-12-11 17:16:41

问题


i try to reload Rewarded Video Ads, when i call RewardedVideoAd.instance.load(adUnitId: "xxx", targetingInfo: xyz); i find below error :

W/MessageQueue(13672): Handler (android.os.Handler) {1a13e8a} sending message to a Handler on a dead thread W/MessageQueue(13672): java.lang.IllegalStateException: Handler (android.os.Handler) {1a13e8a} sending message to a Handler on a dead thread W/MessageQueue(13672): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543) W/MessageQueue(13672): at android.os.Handler.enqueueMessage(Handler.java:643) W/MessageQueue(13672): at android.os.Handler.sendMessageAtTime(Handler.java:612) W/MessageQueue(13672): at android.os.Handler.sendMessageDelayed(Handler.java:582) W/MessageQueue(13672): at android.os.Handler.sendEmptyMessageDelayed(Handler.java:546) W/MessageQueue(13672): at android.os.Handler.sendEmptyMessage(Handler.java:531) W/MessageQueue(13672): at com.google.android.gms.ads.exoplayer1.h.c(:com.google.android.gms.policy_ads_fdr_dynamite@20300003@20300003.251657827.251657827:2) W/MessageQueue(13672): at com.google.android.gms.ads.internal.video.exoplayer1.f.b(:com.google.android.gms.policy_ads_fdr_dynamite@20300003@20300003.251657827.251657827:1) W/MessageQueue(13672): at com.google.android.gms.ads.internal.webview.t.E(:com.google.android.gms.policy_ads_fdr_dynamite@20300003@20300003.251657827.251657827:5) W/MessageQueue(13672): at com.google.android.gms.ads.internal.webview.j.onPageFinished(:com.google.android.gms.policy_ads_fdr_dynamite@20300003@20300003.251657827.251657827:2) W/MessageQueue(13672): at uU.d(PG:307) W/MessageQueue(13672): at aIV.handleMessage(PG:73) W/MessageQueue(13672): at android.os.Handler.dispatchMessage(Handler.java:102) W/MessageQueue(13672): at android.os.Looper.loop(Looper.java:154) W/MessageQueue(13672): at android.app.ActivityThread.main(ActivityThread.java:6780) W/MessageQueue(13672): at java.lang.reflect.Method.invoke(Native Method) W/MessageQueue(13672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) W/MessageQueue(13672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) W/ExoPlayerImplInternal(13672): Sent message(1) after release. Message ignored. D/Graph (13672): removeVertex() : insertDummyVertex, because there is no ancestor. D/ViewRootImpl@b8db50eAdActivity: mHardwareRenderer.destroy()#4 D/ViewRootImpl@b8db50eAdActivity: dispatchDetachedFromWindow

i place the listener in initState() of my screen. in this screen i have a button where if we tap it, it should show Rewarded Video Ads.

Moreover, after getting error when Rewarded Ads reloaded, i got below error after tap the button to show ads (because ads instance was null):

E/flutter (13672): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(ad_not_loaded, show failed for rewarded video, no ad was loaded, null) E/flutter (13672): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7) E/flutter (13672): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33) E/flutter (13672): E/flutter (13672): #2 _invokeBooleanMethod (package:firebase_admob/firebase_admob.dart:518:61) E/flutter (13672): E/flutter (13672): #3 RewardedVideoAd.show (package:firebase_admob/firebase_admob.dart:392:12)

I place Rewarded Video Ads Listener at initState(), below are the codes:

@override
  void initState() {
    super.initState();
...
    RewardedVideoAd.instance.listener =
        (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
      if (event == RewardedVideoAdEvent.completed) {
        setState(() {
          print ("::debug:: ads should be reloaded");
          RewardedVideoAd.instance.load(adUnitId: "ca-app-pub-3940256099942544/5224354917", targetingInfo: targetingInfos);
        });
      }
    };
...

However, if i put code at button's onPressed like below, the video ads will show after tap for 2-3 times (where at debug is show ads = null)

RaisedButton(
  onPressed: () {
    RewardedVideoAd.instance.show().whenComplete(() {
              RewardedVideoAd.instance.load(adUnitId: "ca-app-pub-3940256099942544/5224354917", targetingInfo: targetingInfos);
    })    
},
...

Any Idea ?

Thanks in Advance...


回答1:


Seems like the issue was with the event completed. Check out this code.

MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
  keywords: <String>['flutterio', 'beautiful apps'],
  contentUrl: 'https://flutter.io',
  childDirected: false,
  testDevices: <String>[], // Android emulators are considered test devices
);
bool _loaded = false;

@override
void initState() {
  super.initState();

  // load ad in the beginning
  RewardedVideoAd.instance
      .load(adUnitId: RewardedVideoAd.testAdUnitId, targetingInfo: targetingInfo)
      .catchError((e) => print("error in loading 1st time"))
      .then((v) => setState(() => _loaded = v));

  // ad listener
  RewardedVideoAd.instance.listener = (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
    if (event == RewardedVideoAdEvent.closed) {
      RewardedVideoAd.instance
          .load(adUnitId: RewardedVideoAd.testAdUnitId, targetingInfo: targetingInfo)
          .catchError((e) => print("error in loading again"))
          .then((v) => setState(() => _loaded = v));
    }
  };
}


@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: Text(
        "Loaded = ${_loaded}",
        style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
      ),
    ),
    floatingActionButton: FloatingActionButton(
      // show ad on FAB click
      onPressed: () async {
        await RewardedVideoAd.instance.show().catchError((e) => print("error in showing ad: ${e.toString()}"));
        setState(() => _loaded = false);
      },
    ),
  );
}


来源:https://stackoverflow.com/questions/57212679/flutter-rewarded-video-ads-error-when-reload-ad-not-loaded-show-failed-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!