在xml布局中包含类时,Eclipse查看器无法实例化com.mopub.mobileads.MoPubView

时间:2012-07-02 16:55:07

标签: android eclipse mopub

我能够下载并导入android的moPub库。按照moPub站点上的简单示例后,应用程序在模拟器中正常工作。但是,添加控件后,eclipse查看器无法正确显示屏幕。

xml包括[基于示例]

    <com.mopub.mobileads.MoPubView
        android:id="@+id/adview"
        android:layout_width="fill_parent"
        android:layout_height="50px" />
当我尝试转到“图形布局”选项卡时,

会导致以下错误

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

虽然这比一个showstopper更刺激,但我宁愿工作。非常感谢你提前。

1 个答案:

答案 0 :(得分:1)

为了快速修复MoPub SDK项目中自己编辑MoPubView(假设您以这种方式添加源代码正在与项目的其余部分进行编译),如果是这样,那么在超级调用之后立即添加以下行获取上下文和属性集参数的构造函数:

    if (isInEditMode()) return;

以下是一个较大的代码段,显示上述行的位置:

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

    if (isInEditMode()) return;

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

添加此行(样式风格)会导致异常不被抛出并在我的设备上运行,并且能够在测试中下载一个320x50的横幅广告(到目前为止,我从未向广告模拟器投放广告。 )

我做了这个改变github并做了拉请求。 发布:版本1.14.1.0 https://github.com/mopub/mopub-android-sdk

Android SDK的整体isInEditMode()用法: http://developer.android.com/reference/android/view/View.html#isInEditMode()