I've been developing a game in LibGDX using Mario Zechner's style of development (framework) from his book. An example he used in the book is a game called Mr.Nom. I'm trying to add AdMob ads into the game, but following the more common tutorials for AdMob/LibGDX is difficult because it's different from Mario Zechner's framework.

You can view Mr.Nom's source code here: https://code.google.com/p/beginnginandroidgames2/source/browse/trunk/ch06-mr-nom/src/com/badlogic?spec=svn13&r=6#badlogic%253Fstate%253Dclosed

So, I did some further research to see if there were other people that had similar troubles integrating AdMob into a game like Mr.Nom and found these two links:
http://www.badlogicgames.com/forum/viewtopic.php?f=21&t=982
http://www.badlogicgames.com/forum/viewtopic.php?f=21&t=1548

I started making some additions that Saurav provided, but I'm getting errors now and I don't know what the errors are telling me.

Here are the errors:
http://pastebin.com/JZ9Q1kb4

Here's my AndroidGame.java:

package com.badlogic.androidgames.framework.impl;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.badlogic.androidgames.framework.FileIO;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.shutproject.MainMenu;
import com.badlogic.androidgames.shutproject.R;
import com.badlogic.androidgames.shutproject.R.id;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public abstract class AndroidGame extends Activity implements Game, IActivityRequestHandler {
    AndroidFastRenderView renderView;
    Graphics graphics;
    Input input;
    FileIO fileIO;
    Screen screen;
    WakeLock wakeLock;

    /* Your ad unit id. Replace with your actual ad unit id. */
    private static final String AD_UNIT_ID = "privateofc";
    private static final String PERS_MOB_ID = "LG-LS980-060df4789011d477"; // test on lgg2

    private AdView adView;
    private final static int SHOW_ADS = 1;
    private final static int HIDE_ADS = 0;

    protected Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case SHOW_ADS:
                {
                    adView.setVisibility(View.VISIBLE);
                    break;
                }
                case HIDE_ADS:
                {
                    adView.setVisibility(View.GONE);
                    break;
                }
            }
        }
    };

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

        boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
        int frameBufferWidth = isLandscape ? 480 : 320;
        int frameBufferHeight = isLandscape ? 320 : 480;
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
                                                 frameBufferHeight, Config.RGB_565);

        float scaleX = (float) frameBufferWidth
                       / getWindowManager().getDefaultDisplay().getWidth();
        float scaleY = (float) frameBufferHeight
                       / getWindowManager().getDefaultDisplay().getHeight();

        // *** This line gets you the Game view
        renderView = new AndroidFastRenderView(this, frameBuffer);

        // *** Next, you create the AdMob view
        // *** Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
        // *** String MY_BANNER_UNIT_ID = "abcde123456"; whatever id you get from AdMob
        AdView adView = new AdView(this);
        adView.setAdUnitId(AD_UNIT_ID);
        adView.setAdSize(AdSize.BANNER);

        // *** Now, you need to have both these views on the screen, which means you need a layout.
        // *** I used a relative layout, there may be ways to make this work with other layouts
        RelativeLayout layout = new RelativeLayout(this);

        // *** First, add the game view to the layout. This will use default parameters, which means
        // *** the Game view will take up the whole screen, which is usually what you want. Doing this
        // *** first ensures the game view is under the ad view. Do it in the reverse sequence, and the
        // ** game view will hide the ad view, which is not what you want
        layout.addView(renderView);

        // *** Then, prepare the ad view for where you want it to go on the layout. I wanted the ad
        // *** to be on the bottom right corner. First, you want the view for the ad to only be as large
        // *** as needed. So use WRAP_CONTENT instead of FILL_PARENT
        RelativeLayout.LayoutParams params =
           new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                 RelativeLayout.LayoutParams.WRAP_CONTENT);

        // *** Now set up the rules that tell the layout how to align this view. In my case, I want it
        // *** on the bottom right
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        // *** Then add the view to the layout, using this version of the function that lets you specify
        // *** the layout parameters
        layout.addView(adView, params);

        // *** And finally, you want the activity to use your whole layout, and not just he game view, so:
        setContentView(layout);

        graphics = new AndroidGraphics(getAssets(), frameBuffer);
        fileIO = new AndroidFileIO(this);
        input = new AndroidInput(this, renderView, scaleX, scaleY);
        screen = getStartScreen();

        // *** Delete this line - this will point the activity to the game view, and you'll lose all the work you did
        // *** setting up the layout
        //----setContentView(renderView);----

        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
    }

    public Context context() {
        return this.getApplicationContext();
    }

    @Override
    public void onResume() {
        super.onResume();
        wakeLock.acquire();
        screen.resume();
        renderView.resume();
    }

    @Override
    public void onPause() {
        super.onPause();
        wakeLock.release();
        renderView.pause();
        screen.pause();

        if (isFinishing())
            screen.dispose();
    }

    public Input getInput() {
        return input;
    }

    public FileIO getFileIO() {
        return fileIO;
    }

    public Graphics getGraphics() {
        return graphics;
    }

    public void setScreen(Screen screen) {
        if (screen == null)
            throw new IllegalArgumentException("Screen must not be null");

        this.screen.pause();
        this.screen.dispose();
        screen.resume();
        screen.update(0);
        this.screen = screen;
    }

    public Screen getCurrentScreen() {
        return screen;
    }
}

Can anyone point me into the right direction?

有帮助吗?

解决方案

Your errors indicate that you are referencing Admob classes from Google Play Services, but those classes are not in your application's classpath.

You need to add the Google Play Services library to your project.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top