2

我正在尝试将 mopub 添加到我的应用程序中。我通过 AndroidStudio 插件安装了 sdk。并将其添加到我的 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>

但它给了我这些错误:

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

当我运行应用程序时它崩溃了:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.hroshandel.myapplication/com.example.hroshandel.myapplication.SimpleIntro_FragmentActivity}:android.view.InflateException:二进制 XML 文件第 15 行:膨胀类 com.mopub 时出错.mobileads.mopubview

引起:android.view.InflateException:二进制 XML 文件第 15 行:膨胀类 com.mopub.mobileads.mopubview 时出错

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

引起:java.lang.ClassNotFoundException:在路径上找不到类“com.mopub.mobileads.mopubview”:DexPathList [[zip file“/data/app/com.example.hroshandel.myapplication-4.apk”] ,nativeLibraryDirectories=[/data/app-lib/com.example.hroshandel.myapplication-4, /vendor/lib, /system/lib]]

4

3 回答 3

7

请检查类名的拼写错误,天才在文档中写道:

<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>

但正确的类名是:

    <com.mopub.mobileads.MoPubView> ... </com.mopub.mobileads.MoPubView>
于 2015-05-26T15:08:27.870 回答
3

似乎您必须按照上述答案中的说明修复您的 gradle。但除此之外,我在运行该应用程序时遇到了相同的错误消息,这就是我能够绕过它的方法:

  1. 删除对 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. 取而代之的是,放一个容器。我选择使用 FrameLayout:
    <FrameLayout
    android:id="@+id/adContainer"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_height="50dp">
    </FrameLayout>
  1. 在 Activity 中,我通过实例化 MoPubView 以编程方式加载它,分配并加载我的广告,然后使用 BannerAdListener onBannerLoaded 事件将其添加到 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");
            }
        });
于 2014-12-29T04:27:55.560 回答
0

明显的问题,您是否遵循了设置过程https://dev.twitter.com/mopub/android/getting-started

编辑:

有 GRADLE

复制到您的项目中

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

添加到您的 build.gradle

include ':app', ':mopub-sdk'
dependencies { compile project(':mopub-sdk') }
于 2014-12-06T18:55:44.877 回答