Android 在片段上显示Admob间质

Android 在片段上显示Admob间质,android,admob,Android,Admob,我正在尝试在我的应用程序上添加admob中间广告,虽然我已经成功地在活动上显示了它,但您能告诉我如何在片段上执行吗?多谢各位 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup view = (ViewGroup) inflater.inflate(R.layout.game_fragment, con

我正在尝试在我的应用程序上添加admob中间广告,虽然我已经成功地在活动上显示了它,但您能告诉我如何在片段上执行吗?多谢各位

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.game_fragment, container, false);
    view.setClipChildren(false);
    ((ViewGroup)view.findViewById(R.id.game_board)).setClipChildren(false);
    mTime = (TextView) view.findViewById(R.id.time_bar_text);
    mTimeImage = (ImageView) view.findViewById(R.id.time_bar_image);
    FontLoader.setTypeface(Shared.context, new TextView[] {mTime}, Font.GROBOLD);
    mBoardView = BoardView.fromXml(getActivity().getApplicationContext(), view);
    FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.game_container);
    frameLayout.addView(mBoardView);
    frameLayout.setClipChildren(false);

    // build board
    buildBoard();
    Shared.eventBus.listen(FlipDownCardsEvent.TYPE, this);
    Shared.eventBus.listen(HidePairCardsEvent.TYPE, this);
    Shared.eventBus.listen(GameWonEvent.TYPE, this);

    return view;
}
这是我的活动`公共类MainActivity扩展片段活动{

private ImageView mBackgroundImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Shared.context = getApplicationContext();
    Shared.engine = Engine.getInstance();
    Shared.eventBus = EventBus.getInstance();

    setContentView(R.layout.activity_main);
    mBackgroundImage = (ImageView) findViewById(R.id.background_image);

    Shared.activity = this;
    Shared.engine.start();
    Shared.engine.setBackgroundImageView(mBackgroundImage);

    // set background
    setBackgroundImage();

    // set menu
    ScreenController.getInstance().openScreen(Screen.MENU);
}

@Override
protected void onDestroy() {
    Shared.engine.stop();
    super.onDestroy();
}

@Override
public void onBackPressed() {
    if (PopupManager.isShown()) {
        PopupManager.closePopup();
        if (ScreenController.getLastScreen() == Screen.GAME) {
            Shared.eventBus.notify(new BackGameEvent());
        }
    } else if (ScreenController.getInstance().onBack()) {
        super.onBackPressed();
    }
}

private void setBackgroundImage() {
    Bitmap bitmap = Utils.scaleDown(R.drawable.background, Utils.screenWidth(), Utils.screenHeight());
    bitmap = Utils.crop(bitmap, Utils.screenHeight(), Utils.screenWidth());
    bitmap = Utils.downscaleBitmap(bitmap, 2);
    mBackgroundImage.setImageBitmap(bitmap);
}
private ImageView mBackgroundImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Shared.context = getApplicationContext();
    Shared.engine = Engine.getInstance();
    Shared.eventBus = EventBus.getInstance();

    setContentView(R.layout.activity_main);
    mBackgroundImage = (ImageView) findViewById(R.id.background_image);

    Shared.activity = this;
    Shared.engine.start();
    Shared.engine.setBackgroundImageView(mBackgroundImage);

    // set background
    setBackgroundImage();

    // set menu
    ScreenController.getInstance().openScreen(Screen.MENU);
}

@Override
protected void onDestroy() {
    Shared.engine.stop();
    super.onDestroy();
}

@Override
public void onBackPressed() {
    if (PopupManager.isShown()) {
        PopupManager.closePopup();
        if (ScreenController.getLastScreen() == Screen.GAME) {
            Shared.eventBus.notify(new BackGameEvent());
        }
    } else if (ScreenController.getInstance().onBack()) {
        super.onBackPressed();
    }
}

private void setBackgroundImage() {
    Bitmap bitmap = Utils.scaleDown(R.drawable.background, Utils.screenWidth(), Utils.screenHeight());
    bitmap = Utils.crop(bitmap, Utils.screenHeight(), Utils.screenWidth());
    bitmap = Utils.downscaleBitmap(bitmap, 2);
    mBackgroundImage.setImageBitmap(bitmap);
}
}
`

使用以下代码来显示中间性逆境

InterestitialAd mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad_unit_id));
            AdRequest adRequest = new AdRequest.Builder().build();
            mInterstitialAd.loadAd(adRequest);
            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    mInterstitialAd.show();
                }
            });
这是我的活动 公共类MainActivity扩展了FragmentActivity{

private ImageView mBackgroundImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Shared.context = getApplicationContext();
    Shared.engine = Engine.getInstance();
    Shared.eventBus = EventBus.getInstance();

    setContentView(R.layout.activity_main);
    mBackgroundImage = (ImageView) findViewById(R.id.background_image);

    Shared.activity = this;
    Shared.engine.start();
    Shared.engine.setBackgroundImageView(mBackgroundImage);

    // set background
    setBackgroundImage();

    // set menu
    ScreenController.getInstance().openScreen(Screen.MENU);
}

@Override
protected void onDestroy() {
    Shared.engine.stop();
    super.onDestroy();
}

@Override
public void onBackPressed() {
    if (PopupManager.isShown()) {
        PopupManager.closePopup();
        if (ScreenController.getLastScreen() == Screen.GAME) {
            Shared.eventBus.notify(new BackGameEvent());
        }
    } else if (ScreenController.getInstance().onBack()) {
        super.onBackPressed();
    }
}

private void setBackgroundImage() {
    Bitmap bitmap = Utils.scaleDown(R.drawable.background, Utils.screenWidth(), Utils.screenHeight());
    bitmap = Utils.crop(bitmap, Utils.screenHeight(), Utils.screenWidth());
    bitmap = Utils.downscaleBitmap(bitmap, 2);
    mBackgroundImage.setImageBitmap(bitmap);
}
private ImageView mBackgroundImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Shared.context = getApplicationContext();
    Shared.engine = Engine.getInstance();
    Shared.eventBus = EventBus.getInstance();

    setContentView(R.layout.activity_main);
    mBackgroundImage = (ImageView) findViewById(R.id.background_image);

    Shared.activity = this;
    Shared.engine.start();
    Shared.engine.setBackgroundImageView(mBackgroundImage);

    // set background
    setBackgroundImage();

    // set menu
    ScreenController.getInstance().openScreen(Screen.MENU);
}

@Override
protected void onDestroy() {
    Shared.engine.stop();
    super.onDestroy();
}

@Override
public void onBackPressed() {
    if (PopupManager.isShown()) {
        PopupManager.closePopup();
        if (ScreenController.getLastScreen() == Screen.GAME) {
            Shared.eventBus.notify(new BackGameEvent());
        }
    } else if (ScreenController.getInstance().onBack()) {
        super.onBackPressed();
    }
}

private void setBackgroundImage() {
    Bitmap bitmap = Utils.scaleDown(R.drawable.background, Utils.screenWidth(), Utils.screenHeight());
    bitmap = Utils.crop(bitmap, Utils.screenHeight(), Utils.screenWidth());
    bitmap = Utils.downscaleBitmap(bitmap, 2);
    mBackgroundImage.setImageBitmap(bitmap);
}

}

非常感谢。我将给它一个trytry minterstitalad=newinterstitalad(getContext());