问题
I'm using InterstitialAd by Admob. My app crashes at "loadAd" only in the first time it's called, and it's not reproducible (it happens once in 100-200 runs less or more). the ad unit id is correct for sure. So if loadAd doesn't fail in the first time it's called, it won't fail at all at this run.
class member:
InterstitialAd mInterstitialAd;
onCreate:
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad_unit_id));
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
finish();
}
});
requestNewInterstitial();
requestNewInterstitial method:
private void requestNewInterstitial() {
if (mInterstitialAd != null) {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(getResources().getString(R.string.test_device))
.build();
if (adRequest == null) {
return;
}
// HERE'S THE CRASH
try {
mInterstitialAd.loadAd(adRequest);
}catch(Exception e) {
return;
}
}
}
The catch doesn't catch the crash of "loadAd".
How can I catch it or at least cause it not to crash my app? I wish that when loadAd fails, no ad will be displayed.
LOGCAT:
A/libc: Fatal signal 5 (SIGTRAP), code 1 in tid 27794 (AdWorker(Defaul)
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
I don't know if the last 2 lines are about this crash but the first one is for sure.
回答1:
Sometimes what happen is Ad does to get loaded into your buffer so when you try to call loadAd, it crashes onCreate:
mInterstitialAd = new InterstitialAd(this);
// set the ad unit ID
mInterstitialAd.setAdUnitId(getString(R.string.ad_id));
AdRequest adRequest2 = new AdRequest.Builder()
.build();
// Load ads into Interstitial Ads
mInterstitialAd.loadAd(adRequest2);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
showInterstitial();
}
});
In the same class:
private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
回答2:
Activtiy_m1.java
public class Activity_m1 extends AppCompatActivity {
InterstitialAd interstitial;
Activity mactivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m1);
setTitle("ABCD");
Advertise.displaym1(getApplicationContext());
}
@Override
public void onBackPressed() {
Advertise adv = new Advertise();
adv.disp_adm1();
finish();
}
}
Advertise.java
public class Advertise {
public static InterstitialAd interstitial;
public static void displaym1(Context act) {
interstitial = new InterstitialAd(act);
interstitial.setAdUnitId("ca-app-pub-2621588945556564/3343556333");
AdRequest adRequest1 = new AdRequest.Builder().build();
interstitial.loadAd(adRequest1);
}
public void disp_adm1() {
if (interstitial.isLoaded())
interstitial.show();
}
}
来源:https://stackoverflow.com/questions/43236448/android-admob-interstitialad-crash-at-loadad