我的应用程序的以下代码中出现空指针异常:
@Override
protected void onResume() {
super.onResume(); //To change body of overridden methods use File | Settings | File Templates.
// Create the adView
adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
LinearLayout layout = (LinearLayout) findViewById(R.id.ad_layout);
// Add the adView to it
layout.addView(adView); //NullPointerException here
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean isButtonLabelsEnabled = sp.getBoolean("isButtonLabelsEnabled", false);
if (!isButtonLabelsEnabled) {
stripLabels();
} else {
setContentView(R.layout.languageselection);
}
}
我无法复制这个问题,我已经在模拟器、galaxy S2 设备和 ZTE 刀片上使用了我的应用程序中的所有功能,但我找不到任何问题。
我收到很多用户报告,但他们没有说明原因,只是当他们尝试打开它时它会强制关闭。
也许我没有正确使用android生命周期?
作为一种解决方法,我将 AdMob 的内容封装在 try/catch 中,这样它就可以通过了。
有任何想法吗?