使用LIBGDX设置Admob时出错

时间:2014-10-04 20:44:05

标签: java android eclipse libgdx admob

我遵循了LIBGDX关于设置AdMob的教程:https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx

和我的“AndroidLauncher.java”目前看起来像这样:

package com.theashcode.hfcytGame.android;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.android.gms.ads.*;
import com.theashcode.hfcytGame.Game;


public class AndroidLauncher extends AndroidApplication {

AdView adView;
View gameView;


@Override public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    // Create the layout
    RelativeLayout layout = new RelativeLayout(this);

    // Do the stuff that initialize() would do for you
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    // Create the libgdx View
   gameView = initializeForView(new Game(), config );

----造成问题的线-----

    // Create and setup the AdMob view
    adView = new AdView(this, AdSize.BANNER, "ca-app- pub-000000000000");// Put in your secret key here
    adView.loadAd(new AdRequest());

----导致问题的行结束------

    // Add the libgdx view
    layout.addView(gameView);

    // Add the AdMob view
    RelativeLayout.LayoutParams adParams = 
        new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    layout.addView(adView, adParams);

    // Hook it all up
    setContentView(layout);
}
}

正如我所说,我在这一行上收到错误

// Create and setup the AdMob view
adView = new AdView(this, AdSize.BANNER, "ca-app- pub-000000000000");
adView.loadAd(new AdRequest());    

在第一行显示“构造函数AdView(AndroidLauncher,AdSize,String)未定义

在第二行它说

- “AdView类型中的方法loadAd(com.google.android.gms.ads.AdRequest)不适用于参数(com.google.ads.AdRequest)

- “构造函数AdRequest()不可见”

如果有人能帮助我,我会非常感激,谢谢。我对libGDX很新。

1 个答案:

答案 0 :(得分:1)

问题不在于libGDX,而在于您要导入的广告包

不要将google-play-services.jar放在/ libs /文件夹中。相反,请参考项目中的Google Play服务库。

将库导入工作区。您可以在sdk/extras/google/中找到它..如果您是通过sdk经理下载的。然后通过右键单击您的项目参考=>选择属性=> Android =>添加

然后,您需要进行以下更改:

变化:

adView = new AdView(this, AdSize.BANNER, "ca-app- pub-000000000000");
adView.loadAd(new AdRequest()); 

AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app- pub-000000000000");
AdRequest adRequest = new AdRequest.Builder()
    .build();
adView.loadAd(adRequest);

在清单中的应用程序代码中,更改:

<activity android:name="com.google.ads.AdActivity"/>

要:

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

添加:

<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/>

不要忘记权限:

   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
   <uses-permission android:name="android.permission.INTERNET"/>