问题
I am trying to add ad to my simply game. I' ve followed instructions from here: [url]https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx[/url] . My AndroidLauncher looks like this :
package com.redHoodie.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.google.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.redHoodie.MainHoodie;
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 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
View gameView = initializeForView(new MainHoodie());
// Create and setup the AdMob view
AdView adView = new AdView(this, AdSize.BANNER, "xxxxx"); // 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);
}
}
and this three rows of code are underlined by eclipse :
View gameView = initializeForView(new MainHoodie(),false);
// Create and setup the AdMob view
AdView adView = new AdView(this, AdSize.BANNER, "xxxxx"); // Put in your secret key here
adView.loadAd(new AdRequest());
ecllipse tels me to remove all arguments of these methods, but when i dothis my game crush just after launch. I am using libgdx 1.2
回答1:
You are mixing up the old/deprecated admob api with the new admob api(via google play services). You have:
import com.google.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
You have to stick to one api.
If it's the old, thus libs/GoogleAdMobAdsSdk-6.4.1.jar, you'd have to import the classes from the packages:
import com.google.ads.AdSize;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
and set up the adview:
// Create and setup the AdMob view
AdView adView = new AdView(this, AdSize.BANNER, "xxxxx"); // Put in your secret key here
adView.loadAd(new AdRequest());
If it's the new admob via the google play services library, then you should have:
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
来源:https://stackoverflow.com/questions/25123447/admob-new-adrequest-unrecognized-by-eclipse