Admob Google Play服务。显示插页式广告时出现空指针异常

时间:2013-12-09 13:16:00

标签: java android eclipse admob google-play-services

我正在尝试将 admob 整合到我的游戏中,但是当我尝试展示插页式广告时,我得到nullpointerexcpetion。这是我的代码.. 在 onCreate


interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("a1528e2f9897fc5");

AdRequest adRequest = new AdRequest.Builder().build();

interstitial.loadAd(adRequest);

我在这一行收到错误.. interstitial.loadAd(adRequest);

这是我的日志

12-11 17:12:41.755: E/AndroidRuntime(9357): FATAL EXCEPTION: main
12-11 17:12:41.755: E/AndroidRuntime(9357): java.lang.RuntimeException: Unable to start activity ComponentInfo{cr.logics.fastfood/cr.logics.fastfood.FastFood}: java.lang.NullPointerException
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.os.Looper.loop(Looper.java:137)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.main(ActivityThread.java:4898)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at dalvik.system.NativeStart.main(Native Method)
12-11 17:12:41.755: E/AndroidRuntime(9357): Caused by: java.lang.NullPointerException
12-11 17:12:41.755: E/AndroidRuntime(9357):     at tj.a(SourceFile:191)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at tt.onTransact(SourceFile:81)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.os.Binder.transact(Binder.java:326)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at com.google.android.gms.internal.ac$a$a.a(Unknown Source)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at cr.logics.fastfood.FastFood.onCreate(FastFood.java:245)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.Activity.performCreate(Activity.java:5206)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-11 17:12:41.755: E/AndroidRuntime(9357):     ... 11 more

我已将google-play-services-lib添加到我的项目中,将元数据添加到清单中,将所有内容添加到Google指南中,但我面临此错误,(抱歉我的测试版英文版)。 有什么建议? 提前谢谢!

3 个答案:

答案 0 :(得分:3)

将以下内容添加到项目的proguard-project.txt中,以防止它剥离admob类

-keep public class com.google.android.gms.ads.** {
  public *;
}

-keep public class com.google.ads.** {
  public *;
}

并在清单文件中包含以下活动

enter image description here

android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

答案 1 :(得分:0)

在显示插页式广告之前,您必须检查插页式广告对象是否为空,并检查广告是否已加载。 完整代码如下:

在“活动”的 onCreate()方法中:

public class MainActivity extends Activity {

     public static InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener() {

             @Override
             public void onAdLoaded() {
                  // Code to be executed when an ad finishes loading.
             }

             @Override
             public void onAdFailedToLoad(int errorCode) {
                 // Code to be executed when an ad request fails.
             }

            @Override
            public void onAdClosed() {
                // Load the next interstitial Ad
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }

        });
    }
}

为了展示广告,我使用了静态方法。这样我就可以从其他活动中调用此方法。

 public static void showInterstitialAd() {

        if (mInterstitialAd != null) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {

                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }
        }


    }

答案 2 :(得分:-1)

在显示广告之前,您必须检查广告是否已加载:

if (interstitial!=null && interstitial.isLoaded()) 
{
   interstitial.show();
}