Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Created attachment 2718 [details]
This is a test case using C# in Mono For Android. See internal documentation to trigger failure case.
The Switch (toggle) control throws a null-pointer exception upon adding it programmatically to a view when I have constructed the toggle control with a corresponding style reference. If I do not specify a style upon construction of the control and use APIs to "style" the control instead, then the control may be added successfully to a view through programmatic means. Of course, one can always specify style properties in the layout for the control and it will render fine. But that's not what we currently need at our company. We build most controls programmatically (dynamically) and need to style the control during construction using a style reference.
in my case the app never starts up, even without reaching the code tha tneeds to be commented out.
I believe that the NullPointerException is due to invalid use of Switch. As per the Switch documentation:
> defStyle :
> An attribute ID within the active theme containing a reference to the
> default style for this widget. e.g. android.R.attr.switchStyle.
Meanwhile, you're trying:
var s3 = new Switch(this, null, Resource.Attribute.s1);
While Resource.Attribute.s1 is an attribute ID, it isn't an "attribute ID within the active theme containing a reference to the default style for this widget."
I don't know how to construct such an attribute ID, but I'm pretty sure that Resource.Attribute.s1 is NOT it, given that Resource.Attribute.s1 comes from Resources\values\attrs.xml, and s1 is an <attr/> within a <declare-styleable/>, which appears to be how you declare new XML attributes for View subclasses:
Rephrased, your attrs.xml:
<attr name="s1" format="reference" />
<attr name="s2" format="reference" />
makes it possible to do this in your layout.axml:
Note that you don't have a MyCustomView type, so the above is nonsense, which further adds to my belief that Resource.Attribute.s1 will NOT work as you're attempting to use it.
In investigating all this I've tried to find an existing Java example that constructs a Switch programmatically. Unfortunately, I haven't found one.
If you believe that I'm closing this bug in error, please provide a Java example that demonstrates code-based construction of an android.widget.Switch and provides a non-default value for the Switch constructor's defStyle parameter.
If you create `s3` without providing the defStyle parameter, your sample works as expected:
var s3 = new Style(this, null);