android - ADMOB-java.lang.IllegalStateException: isLoaded must be called on the main UI thread -


i newbie android,its androidlauncherjava file intersial code

       @override public void oncreate (bundle savedinstancestate) {         super.oncreate(savedinstancestate);           relativelayout layout = new relativelayout(this);         androidapplicationconfiguration config = new androidapplicationconfiguration();           requestwindowfeature(window.feature_no_title);         getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,                  windowmanager.layoutparams.flag_fullscreen);         getwindow().clearflags(windowmanager.layoutparams.flag_force_not_fullscreen);          // create libgdx view         view gameview = initializeforview(new mygdxgame(this),config);           adview adview = new adview(this);         adview.setadunitid("ca-app-pub-6916351754834612/9855033027");         adview.setadsize(adsize.banner);         adview.loadad(new adrequest.builder()         .build());           layout.addview(gameview);          // add admob view         relativelayout.layoutparams adparams = new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content);         adparams.addrule(relativelayout.align_parent_bottom, relativelayout.true);         adparams.addrule(relativelayout.center_horizontal, relativelayout.true);         adview.setlayoutparams(adparams);         adview.setbackgroundcolor(color.black);          layout.addview(adview, adparams);          iad = new interstitialad(this);         iad.setadunitid(ad_unit_id);         loadinterstitial();         iad.setadlistener(new adlistener() {             @override             public void onadloaded() {                 }              @override             public void onadfailedtoload(int errorcode) {              }         });         setcontentview(layout);        }      public void loadinterstitial() {         /*adrequest adrequest = new adrequest.builder()         .addtestdevice(adrequest.device_id_emulator)         .addtestdevice("0fd328b10106bd9b2be832163d43d085")         .build();*/         adrequest adrequest = new adrequest.builder().build();         iad.loadad(adrequest);          //iad.loadad(adrequest);     }      public void showinterstitial() {         if (iad.isloaded()) {             iad.show();         } else {             //log.d(tag, "interstitial ad not loaded yet");         }     } 

its code interstitial ads;i getting java.lang.illegalstateexception: isloaded must called on main ui thread .checked few solution here,couldn't fixed .i got know iad not loading.can me, went wrong. thank in advance

you didn't show showinterstitial() being called contains call isloaded!
please check stack trace more clues.

blindly you're calling showinterstitial event listener or other (gdx?) background thread. if that's case have 2 options:

make showinterstitial() fool-proof

public void showinterstitial() {     if(looper.mylooper() != looper.getmainlooper()) {         runonuithread(new runnable() {             @override public void run() {                 doshowinterstitial();             }         });     } else {         doshowinterstitial();     } } private void doshowinterstitial() {     if (iad.isloaded()) {         iad.show();     } else {         //log.d(tag, "interstitial ad not loaded yet");     } } 

make calling code smarter

void mymethodcallingshowinterstitial() {     ... doing other background stuff ...     // replace showinterstitial(); below:     activityreference.runonuithread(new runnable() {         @override public void run() {             activityreference.showinterstitial();         }     });     ... doing other background stuff ... } 

in both of above cases after runonuithread cannot rely on interstitial being shown! if accidentally implement both it's not problem because fool-proof method won't post again ui.

as alternative runonuithread can use handler, more see the official documentation on this.


Comments

  1. I am working on a game which is libgdx based was stuck which loading admod rewarded videos. fool proof saved my day :d

    ReplyDelete

Post a Comment

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

python - ValueError when using pandas.read_json -