使用InterstitialAd admob

时间:2016-07-18 17:40:29

标签: java android android-studio

您好我想运行InterstitialAd每次在我的应用程序中执行名为play(RadioListElement rle)的方法时它会给我这个错误

Class MusicPlayer

public class MusicPlayer extends Context {
private static MediaPlayer mediaPlayer;
private InterstitialAd InterstitialAd;
private AdRequest adsRequest;

public void play(RadioListElement rle) {
    radioListElement = rle;
    playMusic(radioListElement.getUrl());
    adsRequest = new AdRequest.Builder().build();
    /*Prepare the Interstitial Ad*/
    InterstitialAd = new InterstitialAd(this);
    /* Insert the Ad Unit ID */
    InterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    InterstitialAd.loadAd(adsRequest);
    /* Prepare an Interstitial Ad Listener*/
    InterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded () {                
  displayInterstitial();         }        });    }

private void displayInterstitial() {
if (InterstitialAd.isLoaded()) {
InterstitialAd.show();     }    }

日志

  

26421-26421 / com.radio.stations E / GooglePlayServicesUtil:找不到Google Play服务资源。检查项目配置以确保包含资源。   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:java.lang.NullPointerException:尝试调用虚拟方法'android.content.pm.PackageInfo android.content.pm.PackageManager。 null对象引用上的getPackageInfo(java.lang.String,int)'   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.google.android.gms.common.zze.isGooglePlayServicesAvailable(Unknown Source)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.google.android.gms.common.zzc.isGooglePlayServicesAvailable(Unknown Source)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.google.android.gms.ads.internal.util.client.zza.zzar(Unknown Source)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.google.android.gms.ads.internal.client.zzl.zza(Unknown Source)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.google.android.gms.ads.internal.client.zzl.zzb(Unknown Source)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.google.android.gms.ads.internal.client.zzaf.zzam(Unknown Source)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.google.android.gms.ads.internal.client.zzaf.zza(Unknown Source)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.Radio.Stations.MusicPlayer.play(MusicPlayer.java:125)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.Radio.Stations.RadioList.nextOrPreviousRadioStation(RadioList.java:82)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:at com.Radio.Stations.MusicPlayerControl $ 2.onTouch(MusicPlayerControl.java:94)   07-18 16:52:59.049 26421-26421 / com.radio.stations W / System.err:在android.view.View.dispatchTouchEvent(View.java:8802)

build.gradle project com.radio.stations

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.Switzerland.radio.stations"
    multiDexEnabled true
    minSdkVersion 14
    targetSdkVersion 23

}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'        }    }}
dependencies {
compile project(':initActivity')
compile 'com.android.support:support-v4:24.0.0'
compile 'com.google.android.gms:play-services:9.2.0'}

1 个答案:

答案 0 :(得分:1)

您需要传递activity(如此)或application的上下文。你可以试试这个

public class MusicPlayer extends ActionBarActivity 

并且还有一些错误从下一行开始

InterstitialAd = new InterstitialAd(this);

reference variable的{​​{1}}位置。 尝试google guideline for ads,以便更好地理解和清楚示例。