Search code examples
androidnullpointerexceptionmopub

Integrating Mopub ads into Cocos2dx 2.1


The Mopub integration instructions suggest creating the ad view in xml with a snippet like this:

<com.mopub.mobileads.MoPubView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/adview"
android:layout_width="fill_parent"
android:layout_height="50dp"
/> 

Then the instructions suggest creating the ad view in the activity's create method like this:

moPubView = (MoPubView)findViewById(R.id.adview);
moPubView.setAdUnitId("123412341234");
moPubView.loadAd();

Unfortunately moPubView is not created because Cocos2dx 2.1 doesn't use XML for layout anymore. So the app crashes on launch due to a null pointer exception.

I tried passing R.layout.main to setContentView before attempting to create the ad view. This does successfully create the ad view however the rest of the app isn't visible.

How can I create a view like the one above programmatically without using XML?

I think this solve the null pointer exception issue without having to call setContentView.


Solution

  • private static BannerAdManager m_adDelegate = null;
    private static FrameLayout.LayoutParams m_bannerParam = null;
    private static FrameLayout m_appLayout = null;
    
    
    public static void initialize(Activity p_activity)
    {
        // create instance of delegate
        if( m_adDelegate == null )
        {
            m_adDelegate = new BannerAdManager();
        }
    
        m_activity = p_activity;
    
    
        //~~~setup mopub banner ad
        //~~~create mopub ad view
        m_mopubView = new MoPubView(m_activity);
    
        m_bannerParam = new FrameLayout.LayoutParams(   
                            FrameLayout.LayoutParams.WRAP_CONTENT,
                            FrameLayout.LayoutParams.WRAP_CONTENT
                        );
    
        m_mopubView.setAdUnitId(Config_Android._ID);
        m_mopubView.setBannerAdListener(m_adDelegate);
        m_mopubView.loadAd();
    
        //get current layout
        m_appLayout = (FrameLayout) m_activity.findViewById(android.R.id.content);
    
        // set default alignment
        m_bannerParam.gravity = Gravity.TOP | Gravity.CENTER;
    
        //add mopub view to layout
        m_appLayout.addView(m_mopubView, m_bannerParam);
    
    }
    
        public static void setBannerPositionTopWithPadding(float p_padding)
        {
            getActivity().runOnUiThread(new Runnable() 
            {
                public void run()
                {
                    activity = getActivity();
    
                    m_bannerParam.gravity  = Gravity.TOP | Gravity.CENTER;
                    m_bannerParam.width    = FrameLayout.LayoutParams.WRAP_CONTENT;
                    m_bannerParam.height   = FrameLayout.LayoutParams.WRAP_CONTENT;
    
                    m_bannerParam.topMargin = (int) p_padding;
    
                    m_appLayout.updateViewLayout(m_mopubView, m_bannerParam);
                    m_mopubView.refreshDrawableState();
    
                }
            });
        }