0

So I finished my Android game and decided to monetize it with ads. However, I get some strange behaviour from the AdView. My game consists of 2 activities, let's call them activity A and activity B. A is the surfaceview of the game which does not have a .xml file and B is a regular activity for displaying highscores. So I want an ad to appear on A but it doesn't. On the Logcat I get that the request started and after 3 minutes it gives an error code 0 (which from what I understand means there was an internal error). The strange part is that if after the request starts I switch to activity B (A didn't call finish() so it is still loaded in background) and wait for about 10 seconds I get 'Ad finished loading'. Then if I finish B and go back to A, the ad is loaded and displayed normally.

So to sum up, the ad for activity A shows up only if activity B is loaded while the ad is waiting for a reply to the request. In the update at the bottom you will see some code.

I do not know if it is relevant but activity B does have the 'prequisites' to display an ad too (imports, xmlns:ads...) and an AdView declared in its .xml but the java code for displaying the ad is in a comment block. If you want any more information or want me to conduct some tests, tell me. Any help is greatly appreciated.

UPDATE After testing I figured out that even if I remove all the references for ads in activity B, the adview still appears on A as normal. Also, removing the line 'mainLayout.addView(renderView);' fixes the problem but obviously that is not an option. Here's some more code.

// Activity A (SurfaceView)
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    appContext = this.getApplicationContext();
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    adView = new AdView(this);
    adView.setAdUnitId(MY_AD_UNIT_ID);
    Builder adRequest = new AdRequest.Builder();
    adRequest.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
    adView.setAdSize(AdSize.BANNER);
    adView.loadAd(adRequest.build());

    mainLayout = new FrameLayout(this);
    adLayout = new RelativeLayout(this);

    RelativeLayout.LayoutParams lp1 = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams lp2 = new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    adLayout.setLayoutParams(lp2);
    adLayout.addView(adView);
    lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);  
    lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    adView.setLayoutParams(lp1); 


    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    int frameBufferWidth = isPortrait ? 800 : 1280;
    int frameBufferHeight = isPortrait ? 1280 : 800;
    Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float scaleX = (float) frameBufferWidth / metrics.widthPixels;
    float scaleY = (float) frameBufferHeight / metrics.heightPixels;

    GameScreen.deviceWidth = metrics.widthPixels;
    GameScreen.deviceHeight = metrics.heightPixels;

    prefs = this.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);
    prefsExist = prefs.contains("showValueAddition");
    loadDefaultSettings();

    renderView = new AndroidFastRenderView(this, frameBuffer);
    graphics = new AndroidGraphics(getAssets(), frameBuffer);
    fileIO = new AndroidFileIO(this);
    audio = new AndroidAudio(this);
    input = new AndroidInput(this, renderView, scaleX, scaleY);
    screen = getInitScreen(prefs);

    mainLayout.addView(renderView);
    mainLayout.addView(adLayout);
    setContentView(mainLayout);
}

.

// Activity B
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.highscoresactivity);

    prefs = this.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);

    name[0] = (TextView) findViewById(R.id.tvHighscore1N);
    name[1] = (TextView) findViewById(R.id.tvHighscore2N);
    name[2] = (TextView) findViewById(R.id.tvHighscore3N);
    name[3] = (TextView) findViewById(R.id.tvHighscore4N);
    name[4] = (TextView) findViewById(R.id.tvHighscore5N);

    value[0] = (TextView) findViewById(R.id.tvHighscore1V);
    value[1] = (TextView) findViewById(R.id.tvHighscore2V);
    value[2] = (TextView) findViewById(R.id.tvHighscore3V);
    value[3] = (TextView) findViewById(R.id.tvHighscore4V);
    value[4] = (TextView) findViewById(R.id.tvHighscore5V);

    rgGamemode = (RadioGroup) findViewById(R.id.rgGamemode);
    rgDifficulty = (RadioGroup) findViewById(R.id.rgDifficulty);
    btnClearThis = (Button) findViewById(R.id.btnClearThis);
    btnClearAll = (Button) findViewById(R.id.btnClearAll);

    rgGamemode.setOnCheckedChangeListener(this);
    rgDifficulty.setOnCheckedChangeListener(this);

    btnClearThis.setOnClickListener(this);
    btnClearAll.setOnClickListener(this);

    m = GameMode.Arcade;
    d = Difficulty.Easy;
    updateTextViews();

    /*AdView mAdView = (AdView) findViewById(R.id.adView);
    Builder adRequest = new AdRequest.Builder();
    adRequest.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

    mAdView.loadAd(adRequest.build());*/
}

Update 2: There is a class which extends A (I forgot to mention that A's class is abstract) and that class is set as the launcher through the manifest. B is initialised through this method in A:

public static void startHighscoreActivity() {
    Intent i = new Intent("android.intent.action.HIGHSCORE");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    appContext.startActivity(i);
}
Steyiak
  • 64
  • 1
  • 7

0 Answers0