I have a class called BaseKeyListener that extends android.text.method.DigitsKeyListener. I didn't define a constructor in the BaseKeyListener class so the parents default constructor was called.
As of api level 26 the default constructor of DigitsKeyListener is deprecated . In order to still support lower Android versions I would have to add a constructor to BaseKeyListener that conditionally calls the constructor of the parent. However this results in another error.
public static abstract class BaseKeyListener extends DigitsKeyListener
{
public BaseKeyListener()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
// api level 26 constructor
super(null);
}
else
{
// api level 1 constructor (deprecated)
super();
}
}
}
The error I'm getting now:
Call to 'super()' must be first statement in constructor body
I tried a shorthand if statement but that also didn't do the trick. There was another api level 1 constructor but unfortunately it's also deprecated. What can I do to fix these errors?
I would think something like this:
/**
* only use this if you want to use api level 1
*/
public BaseKeyListener() {
super(); // implicitly added already
}
/**
* only use this if you want to use api level 26
* and add if condition before calling this constructor
*/
public BaseKeyListener(Obect param) {
super(param);
}
The deprecated constructor still exists in API 26+ and passing in a null locale is the same as calling the default constructor anyway . You can either just override the default constructor or override both and add a static method to call the right constructor depending on which version of Android it's running on.
public static abstract class BaseKeyListener extends DigitsKeyListener {
public BaseKeyListener() {
super();
}
}
public static abstract class BaseKeyListener extends DigitsKeyListener {
private BaseKeyListener() {
super();
}
private BaseKeyListener(Locale locale) {
super(locale);
}
public static BaseKeyListener newInstance(Locale locale) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return new BaseKeyListener(locale);
} else {
return new BaseKeyListener();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.