Admob in fragments

后端 未结 2 936
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 09:35

I created an empty project with appcompact and tried to add to it the AdMob block

fragment_main.xml



        
相关标签:
2条回答
  • 2021-01-06 10:11

    THIS IS THE ANSWER ... you will write root before the findViewById. That's all

        AdView mAdView = (AdView) root.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    
    0 讨论(0)
  • 2021-01-06 10:13

    You have

    View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
    return rootView; // remove this
    adView = new AdView(getActivity()); // this is unreachable coz you have return above.
    

    The return at the end of onCreateView is fine

    0 讨论(0)
提交回复
热议问题