当我尝试添加插页式广告时,我遇到了一些问题。
似乎Android Studio接受我的代码没有任何错误,但是当我打开我的应用程序时 - 只有一个空白页面和广告横幅(使用FireBase工具安装)。如果我删除代码,一切都恢复正常。如果你能帮助我,我将非常感激。
代码:
package test.com.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdRequest adRequest=new AdRequest.Builder().build();
//Prepare the interstitial Ad
interstitial=new InterstitialAd(MainActivity.this);
//Insert the add unit id
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
interstitial.loadAd(adRequest);
//prepare an interstitial ad listeners
interstitial.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
//Call displayfunction
displayInterstitial();
}
});
}
public void displayInterstitial(){
//if ads are loaded, show interstitial ads
if (interstitial.isLoaded())
{
interstitial.show();
}
}
P.S。添加了字符串和Internet权限。
答案 0 :(得分:0)
看起来您正在尝试显示插页式广告,以响应插页式广告本身的onAdClosed
事件。当用户关闭广告时会触发该事件,这意味着您的代码现在的方式,它永远不会有机会执行(为了关闭插页式广告,必须先显示,你的代码有相反的方式)。
相反,请尝试调用您的displayInterstitial
方法以响应某些UI事件,例如按下按钮,或活动之间的转换,或类似的事情。
另外,我建议在 setListener
之前调用loadAd
。有些AdListener
事件与加载相关,因此最好在调用加载广告之前将其设置到位。