广告没有加载

时间:2014-09-04 21:53:48

标签: java android libgdx admob ads

我按照关于插页式广告的google教程进行了操作,但每当我尝试展示广告时,它都不会被加载。我尝试使用AdListener而不是interstitial.isLoaded,但结果仍然相同。我也给了广告超过2分钟加载几次,所以一定是错的。

我正在使用AVD,这可能是问题吗?当我使用浏览器时,它可以很好地连接到Internet。

删除了错误的链接

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.JrodManU.LaserJumper.android"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="23" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity
            android:name=".AndroidLauncher"
            android:clearTaskOnLaunch="true"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

AndroidLauncher

package com.JrodManU.LaserJumper.android;

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.*;
import com.JrodManU.LaserJumper.GameEventListener;
import com.JrodManU.LaserJumper.LaserJumper;

public class AndroidLauncher extends AndroidApplication implements GameEventListener {
    private InterstitialAd interstitial;
    boolean showed = false;
    boolean loaded = false;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new LaserJumper(this), config);

        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("******");
        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        //.addTestDevice(testDeviceId)
        .build();
        interstitial.loadAd(adRequest);
        interstitial.setAdListener(new AdListener(){
            public void onAdLoaded(){
               loaded = true;
            }
        });
    }

    public boolean displayInterstitial() {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(loaded) {
                    interstitial.show();
                    showed = true;
                } else {
                    System.out.println("failed"); //Always prints failed
                    showed = false;
                }
            }
        });
        return showed;
    }
}

1 个答案:

答案 0 :(得分:2)

如果您收到此错误:

  

无法找到com.google.android.gms.ads.AdActivity,请确认   它在AndroidManifest.xml中声明

然后在您的清单中的<application>标记中添加此活动声明应该修复它:

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