如何从静态片段中显示Admob插页式广告?

时间:2017-12-29 23:31:46

标签: java android android-fragments static admob

这是我的错误;

Non-static method 'showInterstitial()' cannot be referenced from a static context

注意:即使我将interstitialAd设为静态广告,即使广告已加载,它也不会从片段中显示。

我可以从onCreate看到以下公开方法,然后从那里调用createInterstitial()

  public void crateInterstitial(){

        interstitialAd = new InterstitialAd(getApplicationContext());
        interstitialAd.setAdUnitId(MyID);
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // not call show interstitial ad from here
            }

            @Override
            public void onAdClosed() {
                loadInterstitial();
            }
        });
        loadInterstitial();
    }

    public void loadInterstitial(){
        AdRequest interstitialRequest = new AdRequest.Builder()
                .addTestDevice(MyTestDevice)
                .build();
        interstitialAd.loadAd(interstitialRequest);
        Log.e("Loading another","Ad");
    }

    public void showInterstitial(){
        if (interstitialAd.isLoaded()){
            interstitialAd.show();
            Log.e("Showing","Ad");
        }
        else{
            loadInterstitial();
            Log.e("Loading","Ad");
        }

    }

showInterstitial()来自onCreate。但是,我想在用户转到PlaceholderFragment viewPager中的一个特定片段时显示广告。但是,我无法做到这一点。请告诉我如何解决。

     public static class PlaceholderFragment extends Fragment {
            /**
             * The fragment argument representing the section number for this
             * fragment.
             */
            private static final String ARG_SECTION_NUMBER = "section_number";

            public PlaceholderFragment() {
            }

            /**
             * Returns a new instance of this fragment for the given section
             * number.
             */
            public static PlaceholderFragment newInstance(int sectionNumber) {
                PlaceholderFragment fragment = new PlaceholderFragment();
                Bundle args = new Bundle();
                args.putInt(ARG_SECTION_NUMBER, sectionNumber);
                fragment.setArguments(args);
                return fragment;
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {

                showInterstitial(); //This is where the error is.

                View rootView = inflater.inflate(R.layout.fragment_results, container, false); 
                return rootView;
                }

请注意我无法将PlaceHolderFragment设为静态,因为我收到错误;

"此片段内部类应该是静态的(com.xx.PlaceholderFragment)从片段文档:每个片段必须有一个空构造函数,因此可以在恢复其活动状态时进行实例化。强烈建议子类没有带参数的其他构造函数,因为在重新实例化片段时不会调用这些构造函数。相反,参数可以由调用者使用setArguments(Bundle)提供,稍后由Fragment使用getArguments()检索。"

1 个答案:

答案 0 :(得分:0)

我通过创建一个SECOND静态插页式广告对象并从片段中调用以下代码来暂时解决了这个问题。

public static InterstitialAd sinterstitialAd;

sinterstitialAd  = new InterstitialAd(getContext());
                        sinterstitialAd.setAdUnitId(MY_AD_UNIT_ID);
                        final AdRequest sinterstitialRequest = new AdRequest.Builder()
                                .addTestDevice(MY_TEST_DEVICE)
                                .build();
                        sinterstitialAd.loadAd(sinterstitialRequest);
                        Log.e("sinterstitial Ad", "Loading"); 

sinterstitialAd.setAdListener(new AdListener() {
                            @Override
                            public void onAdLoaded() {
                                if (sinterstitialAd.isLoaded()) {
                                    sinterstitialAd.show();
                                    Log.e("Showing", "New Ad");
                                }
                            }

                            @Override
                            public void onAdClosed() {

                            }
                        });