我在以下函数中获得了ArrayIndexOutOfBoundsException异常
private void insertAdMobAd() {
// Create an ad.
adView = AdmobAdHandler.getTopAdView(this);
AdRequest adRequest = AdmobAdHandler.getAdRequest();
if(adView != null && adRequest != null) {
// Add the AdView to the view hierarchy. The view will have no size until the ad is loaded.
adLayout.addView(adView);
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
}
这是堆栈跟踪
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shakti.mathssolver/com.mathssolver.pstrip.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable()
at com.google.android.gms.internal.au.a()
at com.google.android.gms.internal.bh.bi()
at com.google.android.gms.internal.bh.destroy()
at com.google.android.gms.ads.AdView.destroy()
at com.mathssolver.pstrip.BaseSampleActivity.insertAdMobAd(BaseSampleActivity.java:225)
at com.mathssolver.pstrip.BaseSampleActivity.handleTopAdmobAd(BaseSampleActivity.java:199)
at com.mathssolver.pstrip.MainActivity.onCreate(MainActivity.java:104)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
这里要注意的是,我没有在上面提到的方法中调用AdView.destroy()。 destroy()调用在以下函数中进行,但堆栈跟踪在" insertAdMobAd"中显示方法
@Override
public void onDestroy() {
super.onDestroy();
// TODO: adview null here , leads to crash sometimes
if(adView != null)
adView.destroy();
}