在非活动类别中显示admob插页式广告 [英] Showing admob interstitial in non-activity class

查看:88
本文介绍了在非活动类别中显示admob插页式广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏中,我有一个类构造Gameview,并允许用户玩各种关卡,直到他想要为止.现在,我想在关卡结束后显示admob插页式广告. GameView.java此类具有最初启动游戏的方法,该方法是从main活动中调用的.

In my game, I have a class which constructs the Gameview and allows the user to play various levels till he want. Now I want to show an admob interstitial when the level is over. This class GameView.java has method to start the game initially which is invoked from the main activity.

admob插页式代码要求将活动实例作为第一个参数.因此,我将主要活动的实例(使用此关键字)传递给GameView的play方法,但是在调用非页内广告代码时出现以下异常:

The admob interstitial code requires activity instance as the first argument. So I passed an instance of main activity (using this keyword) to the play method of GameView but I get the following exception on invoking the interstitial code:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

我在GameView.java中的代码是:

My code in GameView.java is:

  InterstitialAd interstitial = new InterstitialAd((MainActivity)mContext,
        "admobunitid");
// Create ad request

AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
interstitial.setAdListener((MainActivity)mContext);

GameView.java到目前为止没有扩展任何类,也没有实现任何接口.有什么帮助吗?

GameView.java doesn't extend any class and doesn't implement any interface as of now. Any help on this?

推荐答案

您是从辅助线程中调用它的.您需要从主线程中调用.例如,您可以使用处理程序.

You're calling it from a worker thread. You need to call from within the main thread. You could use a handler, for example.

 activity.runOnUiThread(new Runnable() {
      public void run() {
        InterstitialAd interstitial = new InterstitialAd((MainActivity)mContext,
        "admobunitid");
// Create ad request

AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
interstitial.setAdListener((MainActivity)mContext);
      }
    });

这篇关于在非活动类别中显示admob插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆