如何运行有三个参数版本的构造上pre-蜂窝设备的自定义视图? [英] How to run a custom view with three-argument version constructor on pre-Honeycomb devices?

查看:159
本文介绍了如何运行有三个参数版本的构造上pre-蜂窝设备的自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下contructors扩展的LinearLayout自定义视图:

 公共VoiceRecorderLayout(上下文的背景下)
{
    这(背景下,NULL);
}公共VoiceRecorderLayout(上下文的背景下,ATTRS的AttributeSet){
    这(背景下,ATTRS,0);
    }    公共VoiceRecorderLayout(上下文的背景下,ATTRS的AttributeSet,INT defStyle)
{
    超(背景下,ATTRS,defStyle);
    this.context =背景;
    loadViews();
}

我的应用程序崩溃只有当且仅当我的设备或仿真器的API低于11上运行它。
飞机坠毁的原因是三个参数的构造函数,因为我对Android开发者谷歌小组发现:

  **的LinearLayout构造函数的三参数版本仅适用于11 API和更高 - 即Android 3.0的。
这种依赖性在运行时必须通过设备上运行的实际Android版本来满足。**

有没有我可以使用的旧设备这种观点,如采用Android 2.3.3的方式?

下面是一个logcat的:

  13 11-15:34:20.121:E / AndroidRuntime(408):java.lang.reflect.InvocationTargetException:产生的原因
11-15 13:34:20.121:E / AndroidRuntime(408):在java.lang.reflect.Constructor.constructNative(本机方法)
11-15 13:34:20.121:E / AndroidRuntime(408):在java.lang.reflect.Constructor.newInstance(Constructor.java:415)
11-15 13:34:20.121:E / AndroidRuntime(408):在android.view.LayoutInflater.createView(LayoutInflater.java:505)
11-15 13:34:20.121:E / AndroidRuntime(408):21 ...更多
11-15 13:34:20.121:E / AndroidRuntime(408):java.lang.NoSuchMethodError:所致。android.widget.LinearLayout<&初始化GT;
11-15 13:34:20.121:E / AndroidRuntime(408):在edu.neiu.voiceofchicago.support.VoiceRecorderLayout<&初始化GT;(VoiceRecorderLayout.java:102)
11-15 13:34:20.121:E / AndroidRuntime(408):在edu.neiu.voiceofchicago.support.VoiceRecorderLayout<&初始化GT;(VoiceRecorderLayout.java:97)
11-15 13:34:20.121:E / AndroidRuntime(408):24 ...更多


解决方案

在这样的情况下,很容易不链条的构造函数,并把您的自定义设置code到的init( )风格的方法,让你不必基于平台版本,试图分公司您code。

 公共VoiceRecorderLayout(上下文的背景下){
    超级(上下文);
    初始化(上下文);
}公共VoiceRecorderLayout(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    初始化(上下文);
}公共VoiceRecorderLayout(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    初始化(上下文);
}私人无效的init(上下文的背景下){
    this.context =背景;
    loadViews();
}

I have a custom view that extends LinearLayout with the following contructors:

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();    
}

My application crashes only iff I run it on the devices or emulator with api lower than 11. The reason of the crash is a three argument constructor as I found on android developers google group:

**"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."**

Is there a way that I can use this view in the older devices such as with android 2.3.3?

Here is a 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

解决方案

In cases like this, it is easier not to chain the constructors, and place your custom setup code into an init() style method so that you don't have to try and branch your code based on platform version.

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();    
}

这篇关于如何运行有三个参数版本的构造上pre-蜂窝设备的自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆