【问题标题】:How to Show Admob Interstitial Ad from a Static Fragment?如何从静态片段中显示 Admob 插页式广告?
【发布时间】:2018-06-10 07:17:47
【问题描述】:

这是我的错误;

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

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

我有以下从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 设为静态,因为我收到错误消息;

"这个fragment内部类应该是静态的(com.xx.PlaceholderFragment) 来自Fragment文档:每个fragment都必须有一个空的构造函数,这样才能在恢复其activity的状态时实例化。强烈建议子类不要具有其他带参数的构造函数,因为重新实例化片段时不会调用这些构造函数;相反,参数可以由调用者使用 setArguments(Bundle) 提供,然后由 Fragment 使用 getArguments() 检索。"

【问题讨论】:

    标签: java android android-fragments static admob


    【解决方案1】:

    我通过创建 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() {
    
                                }
                            });
    

    【讨论】:

    • 由于 interstitialAd 被声明为静态,这会造成内存泄漏吗?
    猜你喜欢
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多