0

I try to show the ad outside the MainActivity. If I call showAd in this class it works good and show my ad, but when I call this function in my game class via implemented interface I have an error: "Requires the main thread"

MMRequest request = new MMRequest(); ;  
    MMInterstitial interstitial;
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
        super.onCreate(savedInstanceState);   
        MMSDK.initialize(this);  
        interstitial = new MMInterstitial(this);               
        interstitial.setMMRequest(request);
        interstitial.setApid("xxxxxx");  

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();    

        RelativeLayout layout = new RelativeLayout(this);

        View gameView = initializeForView(new JumpJackieJump(new RequestHandler(), this), cfg);
        layout.addView(gameView);           

        setContentView(layout);  }

@Override
public void showAd() 
{   
    interstitial.fetch();
    interstitial.setListener(new RequestListenerImpl() 
    {   
        @Override
        public void requestCompleted(MMAd mmAd) 
        {
            interstitial.display();             
        }           
});
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

0

This is because the main thread is the UI thread. (Keep in mind this is only for android - For more reference see this).

As per the Android documentation, you should avoid updating the UI outside the UI thread.

Additionally, the Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread.

This being said, you may be able to do what you're looking for with something like this.

Community
  • 1
  • 1
Alexander Kohler
  • 1,907
  • 16
  • 23