问题
I have made a free android game and I wanted to add a banner to it. I followed this tutorial and I don't know why it won't work.
When I run my game on the emulator in LogCat I see a message "Ad is not visible. Not refreshing ad." and when I install the game on my phone I also don't see any ads.
Here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="hidden"
ads:adSize="BANNER"/>
</LinearLayout>
and here is the code in MainActivity.java
public class MainActivity extends AndroidApplication {
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
cfg.useAccelerometer = true;
cfg.useCompass = true;
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
initialize (new RedSquare(), cfg);
}
@Override
public void onPause() {
adView.pause();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
adView.resume();
}
@Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
}
回答1:
To show ads in libgdx you need to initialize your app for view. So, replace all this:
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
initialize (new RedSquare(), cfg);
With this:
RelativeLayout layout = new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
View gameView = initializeForView(new RedSquare(), cfg);
AdView AdView = new AdView(this);
AdView.setAdSize(AdSize.SMART_BANNER);
AdView.setAdUnitId("***"); //The AdUnitId
AdRequest.Builder adRequest = new AdRequest.Builder();
adRequest.addTestDevice("***"); //Your Test device if any
AdView.loadAd(adRequest.build());
layout.addView(gameView);
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);
setContentView(layout);
回答2:
I've found out using adMob
defining the layout explicitely in a file could be a huge headache generator, as sometimes it might turn impredictable with some parameter and layout combination. What always seems to work is adding the AdView
dynamically to some LinearLayout
.
This should work:
final LinearLayout fragLayout = (LinearLayout) inflater.inflate(R.layout.your_layout, container, false);
final AdView adView = new AdView(getActivity());
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);
// Here you'll append the new AdView
final LinearLayout publiView = (LinearLayout) fragLayout.findViewById(R.id.publi);
publiView.addView(adView);
final AdRequest.Builder adReq = new AdRequest.Builder();
// You should include a line like this for testing purposes,
// but only after you've tested whether your AdView works!
// This will prevent your ad being loaded each time you test
// your ad, so it will prevent you being blocked from AdMob.
// You'll find your device_id in the LogCat.
adReq.addTestDevice("your_device_id");
final AdRequest adRequest = adReq.build();
adView.loadAd(adRequest);
---- EDIT ----
This implementation is for Fragment
s, you probably are running an Activity
and that's why you're getting those errors.
Replace
getActivity()
withthis
The
inflater
simply (as it name says) inflates a newLinearLayout
from a layout file. This means I have defined a layout which I've calledyour_layout
and defined inside aLinearLayout
. The inflater just creates an instance of that `LinearLayout. In your case probably it's not necessary.
I'm adding a code that probably will work for you:
final LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_linearlayout_id_where_you_want_to_put_your_adview);
final AdView adView = new AdView(this);
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);
yourLayout.addView(adView);
final AdRequest.Builder adReq = new AdRequest.Builder();
// You should include a line like this for testing purposes,
// but only after you've tested whether your AdView works!
// This will prevent your ad being loaded each time you test
// your ad, so it will prevent you being blocked from AdMob.
// You'll find your device_id in the LogCat.
adReq.addTestDevice("your_device_id");
final AdRequest adRequest = adReq.build();
adView.loadAd(adRequest);
回答3:
There is nothing especially wrong with your layout. Though I would probably change the layout_width for AdView to be:
android:layout_width="match_parent"
But it is not clear what
initialize (new RedSquare(), cfg);
is doing? I suspect it is altering the layout.
来源:https://stackoverflow.com/questions/21996175/ad-is-not-visible-not-refreshing-ad