我正在尝试将InterstitialAd集成到我的应用程序片段之一中。我正在关注它的官方网站,但是如果我输入如下代码,则该代码给我带来错误,例如构造函数InterstitialAd(AuthorListFragment)未定义
The constructor InterstitialAd(MyListFragment) is undefined
我完整的代码如下。
public View onCreateView(LayoutInflater inflater, ViewGroup group,Bundle savedState){
super.onCreateView(inflater,group,savedState);
return inflater.inflate(R.layout.fragment_home,null);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
final AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("7C77ADF1D83FA8B87C0481D6748FA4DB")
.build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("7C77ADF1D83FA8B87C0481D6748FA4DB")
.build();
mInterstitialAd.loadAd(adRequest);
}
});
}
我在此缺少什么?
感谢帮助 :)
最佳答案
public View onCreateView(LayoutInflater inflater, ViewGroup group,Bundle savedState){
super.onCreateView(inflater,group,savedState);
//return inflater.inflate(R.layout.fragment_home,null);
// take this to end
mInterstitialAd = new InterstitialAd(getActivity());
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
final AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("7C77ADF1D83FA8B87C0481D6748FA4DB")
.build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("7C77ADF1D83FA8B87C0481D6748FA4DB")
.build();
mInterstitialAd.loadAd(adRequest);
}
});
return inflater.inflate(R.layout.fragment_home,null);
}