Hirad Roshandel
Hirad Roshandel

Reputation: 2187

ClassNotFoundException mopub in my FragmentActivity

I'm trying to add mopub to my app. I installed the sdk through the AndroidStudio plugin. and added this to my xml

 <com.mopub.mobileads.mopubview
   android:id="@+id/adview"
    android:layout_alignParentBottom="true"
    android:layout_width="fill_parent"
    android:layout_height="50dp">
</com.mopub.mobileads.mopubview>

But it gives me these errors:

The following classes could not be found:
- com.mopub.mobileads.mopubview (Fix Build Path, Create Class)

and when I run the app it crashes:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hroshandel.myapplication/com.example.hroshandel.myapplication.SimpleIntro_FragmentActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class com.mopub.mobileads.mopubview

Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.mopub.mobileads.mopubview

at com.example.hroshandel.myapplication.SimpleIntro_FragmentActivity.onCreate(SimpleIntro_FragmentActivity.java:20)

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mopub.mobileads.mopubview" on path: DexPathList[[zip file "/data/app/com.example.hroshandel.myapplication-4.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.hroshandel.myapplication-4, /vendor/lib, /system/lib]]

Upvotes: 2

Views: 1500

Answers (3)

aorlando
aorlando

Reputation: 668

please check the the typo of the class name, a genius write in the documentation:

<com.mopub.mobileads.mopubview
   android:id="@+id/adview"
    android:layout_alignParentBottom="true"
    android:layout_width="fill_parent"
    android:layout_height="50dp">
</com.mopub.mobileads.mopubview>

but the right class name is:

    <com.mopub.mobileads.MoPubView> ... </com.mopub.mobileads.MoPubView>

Upvotes: 7

t3rse
t3rse

Reputation: 10124

Seems like you'll have to fix your gradle as noted in the above answer. But beyond that I had the same error message running the app and here was how I was able to get around it:

  1. Remove the design time reference to the MoPub adview.
    <com.mopub.mobileads.mopubview
       android:id="@+id/adview"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="50dp">
    </com.mopub.mobileads.mopubview>
  1. In its stead, put a container. I chose to use a FrameLayout:
    <FrameLayout
    android:id="@+id/adContainer"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_height="50dp">
    </FrameLayout>
  1. In the Activity, I loaded the MoPubView programmatically by instantiating it, assigning and loading my ad, and then using the BannerAdListener onBannerLoaded event to add it to the adContainer:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics()); // optional library
        setContentView(R.layout.main_activity);
        ButterKnife.inject(this); // optional, used to inject views e.g. adContainer

        MoPubView moPubView = new MoPubView(this);

        moPubView.setAdUnitId("ad unit id");
        moPubView.loadAd();
        moPubView.setBannerAdListener(new MoPubView.BannerAdListener() {
            @Override
            public void onBannerLoaded(MoPubView moPubView) {
                Log.d(TAG, "Banner loaded");
                try {
                    mAdContainer.addView(moPubView);
                }
                catch(Exception ex){
                    Log.d(TAG, "Error loading banner add to container");
                }
            }

            @Override
            public void onBannerFailed(MoPubView moPubView, MoPubErrorCode moPubErrorCode) {
                Log.d(TAG, "Banner failed");
            }

            @Override
            public void onBannerClicked(MoPubView moPubView) {
                Log.d(TAG, "Banner clicked");
            }

            @Override
            public void onBannerExpanded(MoPubView moPubView) {
                Log.d(TAG, "Banner expanded");
            }

            @Override
            public void onBannerCollapsed(MoPubView moPubView) {
                Log.d(TAG, "Banner collapsed");
            }
        });

Upvotes: 3

peter_budo
peter_budo

Reputation: 1748

Obvious question did you followed setup process https://dev.twitter.com/mopub/android/getting-started

EDIT:

There is GRADLE

Copy into your project

$MY_PROJECT_DIR $ mkdir mopub-sdk
$MY_PROJECT_DIR $ cp -R $MOPUB_DIR/mopub-android-sdk/mopub-sdk mopub-sdk

Add to your build.gradle

include ':app', ':mopub-sdk'
dependencies { compile project(':mopub-sdk') }

Upvotes: 0

Related Questions