2014-09-05 36 views
0

我已将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> 

回答

1

你仍然在XML旧版AdMob视图元素:

<com.google.ads.AdView 
     android:id="@+id/bannerAdView" 
     android:layout_width="fill_parent" 
     android:layout_height="10dp" 
     ads:adUnitId="MyBannerAdId" 
     ads:adSize="BANNER" /> 

与谷歌发挥AdMob的更换(请注意com.google.android.gms.ads.AdView):

<com.google.android.gms.ads.AdView 
    android:id="@+id/bannerAdView" 
    android:layout_width="match_parent" 
    android:layout_height="10dp" 
    ads:adUnitId="MyBannerAdId" 
    ads:adSize="BANNER"/> 

同时检查您导入Google Play AdView的活动代码,而不是旧版代码。

看到这里的指南: https://developers.google.com/mobile-ads-sdk/docs/admob/android/play-migration

+1

谢谢你!我之前发现一篇文章说要在Java文件中检查我的导入,以确保我使用的是Google Play服务库。但我没有想过检查XML元素的来源。当问题的答案有时似乎很简单明显,当它知道在哪里看... – Bryan 2014-09-06 04:25:16

2

您正在使用过时的API(<com.google.ads.AdView)在您的布局xml文件。变化:

<com.google.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ads:adUnitId="MyId" 
     ads:adSize="BANNER" /> 

到:

<com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ads:adUnitId="MY_AD_UNIT_ID" 
     ads:adSize="BANNER"/> 

确保导入正确的类太:

import com.google.ads.AdRequest; 
import com.google.ads.AdView; 

代替:

import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 

您可以按照官方指南在迁移到新的admob here

+0

很好的答案,但4分钟晚;-) – donfuxx 2014-09-05 22:33:34

+1

是的,谢谢娜娜,如果你是第一,我会选择你的答案。 Donfuxx是第一个,他的回答正是我一直在寻找的。 – Bryan 2014-09-06 04:21:41

+0

没问题。最重要的是让你的问题得到解决:) – 2014-09-06 09:51:43