繁体   English   中英

如何解决AdView nullpointerexception错误

[英]How to resolve AdView nullpointerexception errors

我已经将Google Play服务库更新为最新版本。 5.0.8.9,我按照Google开发者网站上的说明制作了AdMob横幅广告。

但是,当我运行该应用程序时,当活动管理器放大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>

您正在布局XML文件中使用已弃用的API( <com.google.ads.AdView )。 更改:

 <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。

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" />

将其替换为google play 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM