这是我的主要活动代码。我想在屏幕上看到的每个按钮上实现广告,当点击时会显示州际广告。但它只显示在btn\ U按钮广告。请指导我如何解决这个问题。
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
private InterstitialAd mInterstitialAd2;
Button btn_button,btn_button2,btn_button3,btn_button4,btn_button5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}});
btn_button=(Button) findViewById(R.id.button);
btn_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i3=new Intent(MainActivity.this,Button.class);
startActivity(i3);
}
});
btn_button2=(Button) findViewById(R.id.button);
btn_button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i2=new Intent(MainActivity.this,Button.class);
startActivity(i2);
}
});
btn_button3=(Button) findViewById(R.id.button);
btn_button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Button.class);
startActivity(i);
}
});
btn_button4=(Button) findViewById(R.id.button);
btn_button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i4=new Intent(MainActivity.this,Button.class);
startActivity(i4);
}
});
btn_button5=(Button) findViewById(R.id.button);
btn_button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i5=new Intent(MainActivity.this,Button.class);
startActivity(i5);
}
});
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
btn_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
mInterstitialAd2 = new InterstitialAd(this);
mInterstitialAd2.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd2.loadAd(new AdRequest.Builder().build());
btn_button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd2.isLoaded()) {
mInterstitialAd2.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
btn_button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
btn_button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
btn_button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
}
}
2条答案
按热度按时间fcg9iug31#
你有相同的按钮id,这就是为什么它只显示一个按钮,正如@anon提到的。
但实际上你不需要那么多的中间示例,因为你只使用一个广告单元。只有一个示例就足以在每个单击的按钮上显示广告。
但是。。。如果您尝试在每次单击时显示广告,您的admob帐户将很快被禁用。
biswetbf2#
您使用的每个按钮都引用相同的按钮id,因此只有一个按钮工作。=)
'btn\u button5=(button)findviewbyid(r.id.button);btn_button4=(button)findviewbyid(r.id.button);'