从18 sep开始,我的应用中遇到了很多崩溃,仅使用4.2.2中的Google Play服务广告。有报告:
java.lang.NullPointerException
at android.webkit.WebSettingsClassic.getDefaultUserAgentForLocale(WebSettingsClassic.java:453)
at android.webkit.WebViewClassic$Factory.getDefaultUserAgent(WebViewClassic.java:1503)
at android.webkit.WebSettings.getDefaultUserAgent(WebSettings.java:1280)
at com.google.android.gms.ads.internal.util.g.a(SourceFile:348)
at com.google.android.gms.ads.internal.n.e.a(SourceFile:275)
at com.google.android.gms.ads.internal.b.<init>(SourceFile:296)
at com.google.android.gms.ads.internal.b.<init>(SourceFile:277)
at com.google.android.gms.ads.AdManagerCreatorImpl.a(SourceFile:36)
at com.google.android.gms.ads.internal.client.o.onTransact(SourceFile:66)
at android.os.Binder.transact(Binder.java:310)
at com.google.android.gms.internal.ar$a$a.a()
at com.google.android.gms.internal.ah.b()
at com.google.android.gms.internal.ah.a()
at com.google.android.gms.internal.au.aM()
at com.google.android.gms.internal.au.a()
at com.google.android.gms.ads.doubleclick.PublisherAdView.loadAd()
at ...
在尝试加载新广告时,您可以看到错误提示。有错误的人知道怎么解决吗?
There is a Google Groups thread但谷歌没有说什么。唯一的时态解决方案是在Android 4.2.2中避免加载广告。
时间解决方案
Kenfoo发布基于处理异常的时态解决方案,并在一段时间(5天)内停用广告:
// Setup your default error handler
static Thread.UncaughtExceptionHandler defaultExceptionHandler = null;
Context context = your context
final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
defaultExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
for (int nest=0;nest<10 && ex!=null;nest++, ex=ex.getCause()) {
StackTraceElement[] stelist = ex.getStackTrace();
for (StackTraceElement ste : stelist) {
if (ste.getClassName().contains("com.google.android.gms.ads")) {
// Save the last time google play ads crashed.
// Next time our app starts, we won't activate ads if it happened
// within the last N days.
SharedPreferences prefs = Settings.getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("admob_crash_time", System.currentTimeMillis());
editor.commit();
break;
}
}
}
if (defaultHandler != null) {
defaultHandler.uncaughtException(thread, ex);
}
}
};
Thread.setDefaultUncaughtExceptionHandler(defaultExceptionHandler);
然后在您的初始化广告的活动启动中:
Context context = your activity or context;
ViewGroup myadframelayout = your frame or parent layout holding the ad;
SharedPreferences prefs = Settings.getPreferences(context);
long lastAdmobCrashTime = prefs.getLong(admob_crash_time, 0);
long timeSinceLastCrash = System.currentTimeMillis() - lastAdmobCrashTime;
// Last crash >= 5 days ago. Try Admob...
if (timeSinceLastCrash >= 5*24*60*60*1000) {
try {
adView = new AdView(context);
adView.setAdUnitId("YOUR ADMOB AD ID");
adView.setAdSize(AdSize.SMART_BANNER);
adView.loadAd(new AdRequest.Builder().build());
myadframelayout.addView(adView);
} catch (Throwable t) {
// No revenue from this guy for today... :(
}
} else {
// Don't run admob. Do whatever you like here.
adView = null;
}
在你的onResume,onPause,等等,如果adView为null则不做任何事情。
我会尝试这个补丁同时谷歌没有解决它。此外,谷歌最终做出了回应,并表示将在几天内推出修复程序。 86%的碰撞属于阿尔卡特One Touch 7041X和7041D。