Main UI Thread error

hi…iam integrating ads with my game and publish it…Iam calling reward() in my sketch when the user clicks an ellipse and reward him…

error: java.lang.IllegalStateException: isLoaded must be called on the main UI thread.
any help will be so much appreciated.please help.

PS:Iam able to display test ads with no issue independently.


public InterstitialAd mInterstitialAd;
public int lives=1;


@ Override
  public void onCreate(Bundle savedInstanceState) {
  print("on create"); 
  super.onCreate(savedInstanceState);
  Window window = getActivity().getWindow();
  RelativeLayout adsLayout = new RelativeLayout(this.getActivity());
  RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
  MobileAds.initialize(this.getActivity(), "ca-app-pub-3940256099942544~3347511713");
  mInterstitialAd = new InterstitialAd(this.getActivity());
  mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
  mInterstitialAd.loadAd(new AdRequest.Builder().build());
    window.addContentView(adsLayout, lp2);

mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
 }

    @Override
    public void onAdFailedToLoad(int errorCode) {
 print(""+errorCode);
      // Code to be executed when an ad request fails.
    }

    @Override
    public void onAdOpened() {
        // Code to be executed when the ad is displayed.
    }

    //@Override
    public void onAdClicked() {
        // Code to be executed when the user clicks on an ad.
    }

    @Override
    public void onAdLeftApplication() {
        // Code to be executed when the user has left the app.
    }

    @Override
    public void onAdClosed() {
        
         mInterstitialAd.loadAd(new AdRequest.Builder().build());
        lives++;
      // Code to be executed when the interstitial ad is closed.
    }
});

}

void reward()
{
  if (mInterstitialAd.isLoaded()) {
      mInterstitialAd.show();
    }
    else
    {
    fill(255);
    text("Ad load to failed or No ads at present",width/2,height/2);
print("fail");
  }
}

Look into threading. I’ve worked with UI in the past and it would be really picky about only manipulating UI in the main thread. This part of the code might be running in a separate thread. Sorry, can’t help that much, I’m not too familiar :confused:

1 Like

Please help, @Akenaton ?

Chrisir

@Yash === , @Chrisir === As for now i have a lot of things to do and cannot get time to fully answer; yet looking to the error there is a problem with the thread which is calling your method; in this case you have to create a runnable for your method (reward(); as soon as possible i will try to do that and post. By the way for trying i must know wether your interstitial is displayed on your phone and how it looks like (put a screenshot);

2 Likes

Thanks for the reply…yes it might be the issue with animation thread and main thread and i dont know how to solve it.

Also reward is called in the draw () method… i get that error during run-time.

PS: This is a test ad that i executed independently without integrating with my code.

@Yash === ok; i think i can have a look to that tomorrow; can you put your Manifest? - Are you using some jar (for google-play-services) in your libs?

I suggest you have a look at this SO post as it addresses your issue. I will leave the else body out during this debugging process as that might cause some trouble - just speculating at this point.

Kf

2 Likes

Iam using the jar which was uploaded here (processing forum)for implementing banner ads…i think the manifest has nothing to do…but i ll post it if it is needed.

Thankyou.i will try to execute it today and will post its verdict.
Initially i thought writing oncreate inside a class will solve the issue like in android studio.So i think I didn’t find this post.thanks again

Just want to ask you to confirm that these are public test keys, and not your personal keys!

Yes .It is a test ad id provided by google

Thanks the link WORKED …Will seek help on other issues if i am not able to solve.thankyou

I was reading about to save a high score in this forum…iam dont know much about io streams…i didn’t get the way to write code for high score…could you please give a sample code to do it.

I suggest you get a minimal version of your feature in Processing Java and then it could potentially be implemented in Android after some modifications. The important thing here is to see your approach and to understand what part is not working for you.
It is not common to implement a feature as you have requested since you did not provided any code. I would be happy to assist as I believe this will be help the Processing community as well. Please ping me when you provide your sample like this: @Yash so I get a notification.

Kf

1 Like

@Yash ===
for saving a score the best way with android is to use the SharedPreferences API which is very easy to implement.More details here: https://developer.android.com/training/data-storage/shared-preferences

1 Like