繁体   English   中英

如何在蜂窝前设备上使用三参数版本构造函数运行自定义视图?

[英]How to run a custom view with three-argument version constructor on pre-Honeycomb devices?

我有一个自定义视图,该视图使用以下构造函数扩展了LinearLayout:

public VoiceRecorderLayout(Context context)
{
    this(context, null);
}

public VoiceRecorderLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    }

    public VoiceRecorderLayout(Context context, AttributeSet attrs, int defStyle) 
{       
    super(context, attrs, defStyle);
    this.context = context;   
    loadViews();    
}

仅当我在api低于11的设备或仿真器上运行它时,我的应用程序才会崩溃。崩溃的原因是我在android开发者google组上找到的三个参数的构造函数:

**"The three-argument version of LinearLayout constructor is only available with API 11 and higher -- i.e. Android 3.0.
This dependency has to be satisfied at runtime by the actual Android version running on the device."**

有没有一种方法可以在旧设备(例如android 2.3.3)中使用此视图?

这是一个logcat:

11-15 13:34:20.121: E/AndroidRuntime(408): Caused by: java.lang.reflect.InvocationTargetException
11-15 13:34:20.121: E/AndroidRuntime(408):  at java.lang.reflect.Constructor.constructNative(Native Method)
11-15 13:34:20.121: E/AndroidRuntime(408):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
11-15 13:34:20.121: E/AndroidRuntime(408):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
11-15 13:34:20.121: E/AndroidRuntime(408):  ... 21 more
11-15 13:34:20.121: E/AndroidRuntime(408): Caused by: java.lang.NoSuchMethodError: android.widget.LinearLayout.<init>
11-15 13:34:20.121: E/AndroidRuntime(408):  at edu.neiu.voiceofchicago.support.VoiceRecorderLayout.<init>(VoiceRecorderLayout.java:102)
11-15 13:34:20.121: E/AndroidRuntime(408):  at edu.neiu.voiceofchicago.support.VoiceRecorderLayout.<init>(VoiceRecorderLayout.java:97)
11-15 13:34:20.121: E/AndroidRuntime(408):  ... 24 more

在这种情况下,不链接构造函数,并将自定义安装代码放入init()样式方法中会更容易,这样您就不必尝试根据平台版本分支代码。

public VoiceRecorderLayout(Context context) {
    super(context);
    init(context);
}

public VoiceRecorderLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public VoiceRecorderLayout(Context context, AttributeSet attrs, int defStyle) {       
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context) {
    this.context = context;   
    loadViews();    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM