开发者

Beginning Android Games and AdMob

开发者 https://www.devze.com 2023-03-27 20:17 出处:网络
I just started with the book: Beginning Android Games and I have a problem already. In the example game we don\'t use main.xml but without that I can\'t implemented AdMob.

I just started with the book: Beginning Android Games and I have a problem already. In the example game we don't use main.xml but without that I can't implemented AdMob.

Did anybody read this book and successfully implemented AdMob in their Game?

Here is my code:

public abstract class AndroidGame extends Activity implements Game {开发者_开发百科
    private AdView adView;
    AndroidFastRenderView renderView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        renderView = new AndroidFastRenderView(this, frameBuffer);
        setContentView(renderView);


        adView = new AdView(this, AdSize.BANNER, "a14e3af977eb71a");

        LinearLayout layout = (LinearLayout)findViewById(R.id.admob_test);

        layout.addView(adView);

        adView.loadAd(new AdRequest());

    }

I hope someone can help me.


If you look at the line

setContentView(renderView);    

You are setting the content view to whatever renderview is. That class extends SurfaceView. That means you are setting a SurfaceView as the content view, not LinearView or RelativeView.

If you do a quick google for "admob and surfaceview", you might come across this link:

Admob on surfaceview

I got this working on my app by doing the following:

    adView = new AdView(this, AdSize.BANNER, "<enter your admob ID here>");
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adView.setLayoutParams(lp);

    RelativeLayout layout = new RelativeLayout(this);
    layout.addView(renderView);
    layout.addView(adView);
    adView.loadAd(new AdRequest());

    setContentView(layout);
    //setContentView(renderView);

I didn't need to change the layout in the xml file.

I spent a good day trying to figure this out. It's a tough one but I hope this helps.


you don't have to use main.xml to implement adMob, you can also create an adView programmatically in Java

edit: check the link again, everything is there:

The five lines of code it takes to add a banner:

  1. Import com.google.ads.*
  2. Declare an AdView instance
  3. Create it, specifying a unit ID—your AdMob publisher ID
  4. Add the view to the UI
  5. Load it with an ad


I suppose you defined a layout in XML in which you have a LinearLayout with id admob_test, you're adding the adView to that layout, good.

But then before that you set your activity to use AndroidFastRenderView as the ContentView, so the LinearLayout is not displayed.

Try to add the AdView directly to your main View, or set your activity to use your LinearLayout as the content View.

0

精彩评论

暂无评论...
验证码 换一张
取 消