# Main UI Thread error

**URL:** https://discourse.processing.org/t/main-ui-thread-error/14386
**Category:** Processing for Android
**Created:** [October 3, 2019, 6:22am UTC](https://discourse.processing.org/t/main-ui-thread-error/14386 "2019-10-03T06:22:53Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Yash](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@Yash](https://discourse.processing.org/u/Yash)
#### Post date: [October 3, 2019, 6:22am UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/1 "2019-10-03T06:22:53Z")

</div>

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.**

```auto

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");
  }
}

```

---

<div class="post-metadata">

### Author: ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)
#### Post date: [October 3, 2019, 5:52pm UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/2 "2019-10-03T17:52:16Z")

</div>

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 😕

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 4, 2019, 9:11pm UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/4 "2019-10-04T21:11:41Z")

</div>

Please help, @Akenaton ?

Chrisir

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [October 5, 2019, 1:08pm UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/5 "2019-10-05T13:08:04Z")

</div>

@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);

---

<div class="post-metadata">

### Author: ![Yash](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@Yash](https://discourse.processing.org/u/Yash)
#### Post date: [October 5, 2019, 2:09pm UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/6 "2019-10-05T14:09:34Z")

</div>

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

 ![video](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4d11256560b72227bc22e867ef48071e247ff628.png)

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.

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [October 5, 2019, 3:05pm UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/7 "2019-10-05T15:05:43Z")

</div>

@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?

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [October 6, 2019, 4:29am UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/8 "2019-10-06T04:29:05Z")

</div>

I suggest you have a look at this [SO post](https://stackoverflow.com/questions/28501787/admob-interstitial-and-error-isloaded-must-be-called-on-the-main-ui-thread) 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

---

<div class="post-metadata">

### Author: ![Yash](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@Yash](https://discourse.processing.org/u/Yash)
#### Post date: [October 6, 2019, 10:10am UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/9 "2019-10-06T10:10:32Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Yash](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@Yash](https://discourse.processing.org/u/Yash)
#### Post date: [October 6, 2019, 10:12am UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/10 "2019-10-06T10:12:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [October 7, 2019, 12:18am UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/12 "2019-10-07T00:18:08Z")

</div>

> [@Yash](#):
>
> ca-app-pub-3940256099942544/1033173712

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

---

<div class="post-metadata">

### Author: ![Yash](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@Yash](https://discourse.processing.org/u/Yash)
#### Post date: [October 7, 2019, 6:41am UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/13 "2019-10-07T06:41:15Z")

</div>

Yes .It is a test ad id provided by google

---

<div class="post-metadata">

### Author: ![Yash](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@Yash](https://discourse.processing.org/u/Yash)
#### Post date: [October 7, 2019, 1:24pm UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/14 "2019-10-07T13:24:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Yash](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@Yash](https://discourse.processing.org/u/Yash)
#### Post date: [October 8, 2019, 3:46am UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/15 "2019-10-08T03:46:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [October 9, 2019, 11:38am UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/16 "2019-10-09T11:38:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [October 9, 2019, 4:17pm UTC](https://discourse.processing.org/t/main-ui-thread-error/14386/18 "2019-10-09T16:17:48Z")

</div>

@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](https://developer.android.com/training/data-storage/shared-preferences)
