Cocos2dx 2.1 and Mopub integration on Android

Hi all,

I’m fairly new to Cocos2dx. Loving it so far! Thanks to all who have made it possible!

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. I think this is 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.

Anyone have advice on how to solve this in Cocos2dx 2.1?

One thought I have is creating the view programmatically instead of using XML. However after some research it looks like I still need to call setContentView. I’m worried I’ll run into the same problem as above hence I thought I’d ask here.

This conversation seems to be talking about the same thing: https://github.com/mopub/mopub-client/issues/85

You can add MoPubView at init function in Cocos2dxActivity.java.

Ok thanks for that tip.

So I dug into the problem a bit more. One strange behavior I’m seeing: running my game in the Android emulator immediately after following step one of Mopub’s integration instructions “Import the MoPub archive file into your Eclipse workspace” causes the app to just be blank. Note before this import the app appears correctly. Any idea why this is happening?

I’m guessing it’s related to Cocos2dx configuring “views” in a way that Mopub doesn’t expect. I’m more familiar with iOS development so a lot of the Android stuff is still foreign to me.

Just an update: the app was “appearing blank” because some network requests were failing. Adding the appropriate permissions to the manifest in the MoPubSDK fixed this. Apparently having the permissions in my project’s manifest was not sufficient.

J P wrote:

Just an update: the app was “appearing blank” because some network requests were failing. Adding the appropriate permissions to the manifest in the MoPubSDK fixed this. Apparently having the permissions in my project’s manifest was not sufficient.

Hi JP,

I’m trying to use Mopub with cocos2d-x 2.1+ too , but I meet the problem. Can you share about how to make the view without xml ?

thanks

Unfortunately I never really got it to work. I switched to interstitial full-screen ads and didn’t run into the same problem. Good luck!