如何通过单击新选项卡显示我的非页内广告?

时间:2017-04-01 13:32:03

标签: java android admob advertising interstitial

在我的MainActivity onCreate中加载插页式广告,如下所示:

AdRequest adRequest = new AdRequest.Builder().build();

mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId("MY ID");
mInterstitialAd.loadAd(adRequest);

我的MainActivity中也有这段代码:

    public void displayInterstitial() {

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                }
            }
        });
    }
}

现在,如果我尝试在我的片段onCreateView中说displayInterstitial();,就像这样:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView=inflater.inflate(R.layout.lustiges_layout,container,false);

    displayInterstitial();


    myGridView = (GridView)rootView.findViewById(R.id.gridView3);
    myGridView.setAdapter(new CustomGridAdapter(getActivity(), items3));

    return rootView;
}

它说“无法解决有问题的'displayInterstitial()'” 片段打开时如何显示插页式广告?

1 个答案:

答案 0 :(得分:0)

如果MainActivity是您要调用fragment方法的displayInterstitial();的父活动,那么您可以这样做。

((MainActivity)getActivity()).displayInterstitial();