我已将Google Play服务库更新为最新版本。 5.0.8.9,我按照Google开发者网站上的说明制作AdMob横幅广告。如何解决AdView nullpointerexception错误
但是,当我运行该应用程序时,当AdView视图被活动管理器充满时,我得到一个java nullpointerexception。
如果我在XML中或运行时在Java代码中创建AdView,则无关紧要。
这是我的活动的OnResume代码,发生错误。
@Override
protected void onResume() {
super.onResume();
final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (connectionStatusCode == ConnectionResult.SUCCESS) {
adView = (AdView) this.findViewById(R.id.bannerAdView);
AdRequest adRequest = new AdRequest
.Builder()
.build();
adView.loadAd(adRequest);
}
}// end onResume
我的活动的XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/yourbasiclayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="2dp"
android:paddingRight="2dp"
>
<TextView android:id="@+id/myAdView_Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold"
android:text="Google Ads"/>
<com.google.ads.AdView
android:id="@+id/bannerAdView"
android:layout_width="fill_parent"
android:layout_height="10dp"
ads:adUnitId="MyBannerAdId"
ads:adSize="BANNER" />
</LinearLayout>
谢谢你!我之前发现一篇文章说要在Java文件中检查我的导入,以确保我使用的是Google Play服务库。但我没有想过检查XML元素的来源。当问题的答案有时似乎很简单明显,当它知道在哪里看... – Bryan 2014-09-06 04:25:16