插页式广告不会让应用启动

时间:2015-08-07 07:14:38

标签: android google-play-services interstitial

我遇到了一个问题:我正在尝试在我的应用中添加插页式广告,我认为这很容易,因为我已经有了常规广告并且效果很好,但出了点问题......

这是我的代码( in_app_billing_system ):

 import com.google.android.gms.ads.AdListener;
 import com.google.android.gms.ads.AdRequest;
 import com.google.android.gms.ads.AdSize;
 import com.google.android.gms.ads.AdView;
 import com.google.android.gms.ads.InterstitialAd;
 ...
 ...
 ...
 mInterstitialAd = new InterstitialAd(this);
 mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
 AdRequest adRequestI = new AdRequest.Builder().addTestDevice("bla bla bla").build();
 mInterstitialAd.loadAd(adRequestI);

这是我的错误:

  

08-07 10:03:00.495 13565-13565 /? E / AndroidRuntime:致命异常:主要       过程:****,PID:13565       java.lang.RuntimeException:无法启动活动ComponentInfo {*****。ActMain}:java.lang.NullPointerException               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)               在android.app.ActivityThread.access $ 800(ActivityThread.java:144)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1246)               在android.os.Handler.dispatchMessage(Handler.java:102)               在android.os.Looper.loop(Looper.java:212)               在android.app.ActivityThread.main(ActivityThread.java:5135)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:877)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)               at dalvik.system.NativeStart.main(Native Method)        引起:java.lang.NullPointerException               在android.content.ContextWrapper.getPackageManager(ContextWrapper.java:94)               在com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(未知来源)               在com.google.android.gms.ads.internal.util.client.zza.zzP(未知来源)               在com.google.android.gms.ads.internal.client.zze.zzb(未知来源)               在com.google.android.gms.ads.internal.client.zzz.zzL(未知来源)               在com.google.android.gms.ads.internal.client.zzz.zza(未知来源)               在com.google.android.gms.ads.InterstitialAd.loadAd(未知来源)               at ****。InAppBillingSystem。(InAppBillingSystem.java:46)               在****。ActMain.onCreate(ActMain.java:59)               在android.app.Activity.performCreate(Activity.java:5231)               在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)               在android.app.ActivityThread.access $ 800(ActivityThread.java:144)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1246)               在android.os.Handler.dispatchMessage(Handler.java:102)               在android.os.Looper.loop(Looper.java:212)               在android.app.ActivityThread.main(ActivityThread.java:5135)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:877)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)               在dalvik.system.NativeStart.main(本地方法)

我尝试使用here中的原始Google代码,但结果是一样的。

主要活动中第59行的代码:

  //INSTALL APP
    if (IS_FIRST_START) tablesInit();

    if (loadFromPref("registered", "").equals("yes")) {
        is_purchased = true;
    } else {
        is_purchased = false;
 **59:    in_app_billing_system = new InAppBillingSystem(this);**
    }

我从未使用过这类广告,也许我错过了什么?

P.S。不要与名称“in_app_billing_system”混淆,这是旧时代,现在我没有应用程序,但广告在那里。

1 个答案:

答案 0 :(得分:0)

这是由于这条线:

mInterstitialAd = new InterstitialAd(this);

相反,我应该使用的是:

mInterstitialAd = new InterstitialAd(myActivity);

问题已经解决了。