自定义视图CustomView没有使用2或3个参数的视图构造函数。

4
我知道我可以按照警告提示的方式去做;创建一个包含2个或3个参数的View构造函数。
然而,CustomView是来自第三方SDK的,并且是final的,所以我觉得我必须使用这个来充分利用SDK。无论如何,没有机会扩展final CustomView。
有什么变通方法吗?
顺便说一下,我正在尝试使用的视图是MetaioSurfaceViewMetaio提供。
以下是崩溃后的logcat信息:
A/WTF(18270): Unable to start activity ComponentInfo{com.suzi.application/com.suzi.application.CustomActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.metaio.sdk.MetaioSurfaceView
A/WTF(18270): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.suzi.application/com.suzi.application.CustomActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.metaio.sdk.MetaioSurfaceView
A/WTF(18270):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
A/WTF(18270):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
A/WTF(18270):   at android.app.ActivityThread.access$700(ActivityThread.java:165)
A/WTF(18270):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
A/WTF(18270):   at android.os.Handler.dispatchMessage(Handler.java:99)
A/WTF(18270):   at android.os.Looper.loop(Looper.java:176)
A/WTF(18270):   at android.app.ActivityThread.main(ActivityThread.java:5455)
A/WTF(18270):   at java.lang.reflect.Method.invokeNative(Native Method)
A/WTF(18270):   at java.lang.reflect.Method.invoke(Method.java:525)
A/WTF(18270):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
A/WTF(18270):   at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
A/WTF(18270):   at dalvik.system.NativeStart.main(Native Method)
1个回答

8

如果您有一个自定义视图,并且CustomView只有一种类型的构造函数,例如默认构造函数

当您将自定义视图添加到xml中时,xml需要以下类型的构造函数:CustomView(Context context, AttributeSet attrs)CustomView(Context context, AttributeSet attrs, int defStyle)

如果您的自定义视图属于第三方库,则只能通过默认构造函数new CustomView();来添加Java代码。


所以除了我必须通过硬编码来处理布局问题外,缺少构造函数(带有AttributeSet参数)并不是一件坏事吗? - Attacktive
1
哈哈,是的,在每个第三方自定义视图中,至少应该有三个构造函数。 - Amit Kumar
很高兴听到没有其他问题。谢谢! - Attacktive

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接