I am confused (again) by the annotations and how to define them.
Here’s my problem:
I want to extend android.preference.PreferenceCategory
The java class has 3 different constructors.
One with just a Context, that’s the one you call when you want to create an instance from code.
The two others are used when inflating an xml layout. One takes an AttributeSet and the other an AttributeSet and a style.
I want to be have my CollapsiblePreferenceCategory extend PreferenceCategory, and I want to be able to create an instance from code
(with just a Context) or from an xml layout.
Since I can only have one constructor in my kotlin class, I would just have the AttributeSet arg take null as a default value.
That’s ok, if you look a the implementation of the constructor with just the Context in PreferenceCategory, it just calls this(context, null).
So far, so good.
My problem is that my code doesn’t compile, because by default, the AttributeSet arg is considered to be non-null.
If I write this:
public class CollapsiblePreferenceCategory(context: Context?, attrs: AttributeSet?): PreferenceCategory(context, attrs) {
it complains that attrs should not be nullable (as expected, the signature by default uses non-nullables).
As a result, I want to add an annotation on the PreferenceCategory constructor to specify that AttributeSet should really be nullable.
I browse to the source and select “specify custom kotlin signature”, and replace:
fun PreferenceCategory(context: Context, attrs: AttributeSet)
with this:
fun PreferenceCategory(context: Context, attrs: AttributeSet?)
But then, instead of getting a K in the margin, I get an exclamation point that says:
Alternate signature is available for this method:
fun PreferenceCategory(context: Context, attrs: AttributeSet?)
It has the following error:
Auto-type ‘android.util.AttributeSet’ is not-null, while type in alternative signature is nullable: ‘AttributeSet?’
And when I try to compile, it’s as if the annotation wasn’t there.
Am I doing something wrong?
Is it a bug? If so is there a workaround?