Aviral
Aviral

Reputation: 1141

Eclipse viewer unable to instantiate com.mopub.mobileads.MoPubView when including the class in an xml layout

I was able to download and import the moPub libraries for android. After following the simple example on the moPub site, the application works fine in the emulator. However, the eclipse viewer is unable to show the screen properly after adding the control.

The xml include [based on the example]

    <com.mopub.mobileads.MoPubView
        android:id="@+id/adview"
        android:layout_width="fill_parent"
        android:layout_height="50px" />

results in the below error when I try to go to the "Graphical Layout" tab

The following classes could not be instantiated:
- com.mopub.mobileads.MoPubView (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse

While this is more of an irritant than a showstopper, it is something I would rather have working. Thank you very much in advance.

Upvotes: 2

Views: 949

Answers (1)

Hunter-Orionnoir
Hunter-Orionnoir

Reputation: 2073

For a quick fix edit the MoPubView yourself in the MoPub SDK project (assuming you added in such a way the source is compiling with the rest of your project) and if so then add the following line right after the super call in the constructor that takes the context and attributes set parameters:

    if (isInEditMode()) return;

Here is a larger snippet showing the placement of the above line:

public MoPubView(Context context, AttributeSet attrs) {
    super(context, attrs);

    if (isInEditMode()) return;

    mContext = context;
    //[...the rest of the constructor's code...]
}

Adding this line (style to taste) caused the exceptions to not be thrown and ran on my device and was able to pull down a 320x50 banner ad in test (I've never got an ad served to a simulator to date.)

I made this change github and made a pull request. Release: version 1.14.1.0 https://github.com/mopub/mopub-android-sdk

Android SDK's take on the whole isInEditMode() usage: http://developer.android.com/reference/android/view/View.html#isInEditMode()

Upvotes: 1

Related Questions