2

I can't seem to figure out how to display ads on my app while currently, instead of setting my content view to the layout file, I am setting my view to a custom class which draws a canvas. I have the following:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        setContentView(new GamePanel(this));
    }

Of course I get a null pointer error. Of course this happens because I edit the AdViews before setting the content view, but even if I switch the order it doesn't work (even without an error I am worried it will just be hidden behind my canvas). Here is my GamePanel class.

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
    public GamePanel(Context context) {
        super(context);

        getHolder().addCallback(this);

        thread = new MainThread(getHolder(), this);
        thread.run();

        setFocusable(true);
     }
     @Override
     public void draw(Canvas canvas) {
        super.draw(canvas);
        //draw a bunch of gameobjects...
     }
     public void update() {
         //update objects
     }
     //required methods...
}

And essentially in the main thread I just loop through frame cycles, call the GamePanel's update() and draw(Canvas canvas) methods and then use surfaceHolder.unlockCanvasAndPost(canvas); (where surfaceHolder is the one corresponding to the GamePanel) to draw the canvas to the screen. I don't use Android Studio's view structure, so I don't know how to implement an Ad View.

  • so did you try without 'GamePanel' and use xml layout with Adview first? – Minh Bui Jun 29 '16 at 03:23
  • did you try putting both: `GamePanel` and `AdView` into the xml layout and using it in `setContentView` ? Say, using the `FrameLayout` as a parent display `AdView` at the top of `GamePanel` (which is just a `SurfaceView`) – x0r Jun 29 '16 at 03:28
  • @x0r no I haven't. I can try working on that, how do I put my custom class into the XML layout though? – wfish454 Tutorials Jun 29 '16 at 03:38
  • Just like any other component, but use a full src path to your class, like this: `` (don't forget required attributes) You can check [this](http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml) for example – x0r Jun 29 '16 at 03:46

0 Answers0