-1

I made a Flappy Bird remake and I just got done with it not too long ago, everything worked as expected, but when I try to add Google's ads, the ads display, but the game doesn't execute and run, it's just a black screen. I'm using LibGDX for my game also. Here's the MainActivity.java code and Main.xml code.

MainActivity.java :

    package com.kensleeinc.SuperBird;

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.kilobolt.ZombieBird.R;
import com.kilobolt.ZombieBird.ZBGame;

public class MainActivity extends AndroidApplication 
{
    private AdView adView;

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

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = false;

        initialize(new ZBGame(), cfg);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
    }
}

Main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/ad_unit_id"/>

</RelativeLayout>

Can anyone help me with this? Thanks!

Heres my log.txt -

11-28 19:41:07.521: E/AndroidRuntime(2889): FATAL EXCEPTION: main
11-28 19:41:07.521: E/AndroidRuntime(2889): Process: com.kilobolt.ZombieBird, PID: 2889
11-28 19:41:07.521: E/AndroidRuntime(2889): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kilobolt.ZombieBird/com.kensleeinc.SuperBird.MainActivity}: java.lang.NullPointerException
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.app.ActivityThread.access$900(ActivityThread.java:173)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.os.Looper.loop(Looper.java:136)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.app.ActivityThread.main(ActivityThread.java:5579)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at java.lang.reflect.Method.invoke(Method.java:515)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at dalvik.system.NativeStart.main(Native Method)
11-28 19:41:07.521: E/AndroidRuntime(2889): Caused by: java.lang.NullPointerException
11-28 19:41:07.521: E/AndroidRuntime(2889):     at com.kensleeinc.SuperBird.MainActivity.onCreate(MainActivity.java:26)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.app.Activity.performCreate(Activity.java:5451)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
11-28 19:41:07.521: E/AndroidRuntime(2889):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
11-28 19:41:07.521: E/AndroidRuntime(2889):     ... 11 more

kenslee123
  • 11
  • 3

2 Answers2

0

your exception is caused by two calls of super.onCreate(savedInstanceState);

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  //Here ***

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = false;

        initialize(new ZBGame(), cfg);

        super.onCreate(savedInstanceState); //and Here ***
        setContentView(R.layout.main);
        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
    }

your onCreate() method must be something similar to:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = false;    
        initialize(new ZBGame(), cfg);

        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
    }
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

I fixed it, it was that the ad was trying to override the game from displaying, therefore one will display and the other won't. Here's the onCreate method that worked for me for LibGDX.

public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
       cfg.useGL20 = false;
       //setContentView(R.layout.main);
       RelativeLayout layout = new RelativeLayout(this);
       RelativeLayout.LayoutParams gameViewParams =
               new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                       RelativeLayout.LayoutParams.WRAP_CONTENT);
       //gameViewParams.bottomMargin = 150;
       requestWindowFeature( Window.FEATURE_NO_TITLE );
       View gameView = initializeForView(new ZBGame(), cfg);
       layout.addView(gameView, gameViewParams);

       AdView adView = new AdView(this);
       adView.setAdUnitId("my-ad-unit-here");
       adView.setAdSize(AdSize.BANNER);

       RelativeLayout.LayoutParams adParams = 
           new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                   RelativeLayout.LayoutParams.WRAP_CONTENT);
       adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
       adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

       layout.addView(adView, adParams);

       AdRequest adRequest = new AdRequest.Builder().build();
       adView.loadAd(adRequest);

       setContentView(layout);    
    }
madlymad
  • 6,367
  • 6
  • 37
  • 68
kenslee123
  • 11
  • 3