Bug 7743 - Switch throws null-pointer exception upon adding it programmatically to a view
Summary: Switch throws null-pointer exception upon adding it programmatically to a view
Status: RESOLVED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.2.x
Hardware: Macintosh Mac OS
: High major
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2012-10-09 11:39 UTC by Scott
Modified: 2014-01-21 16:09 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
This is a test case using C# in Mono For Android. See internal documentation to trigger failure case. (608.97 KB, application/zip)
2012-10-09 11:39 UTC, Scott
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:
Status:
RESOLVED INVALID

Description Scott 2012-10-09 11:39:20 UTC
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.
Comment 1 Miguel de Icaza [MSFT] 2013-05-22 15:34:06 UTC
in my case the app never starts up, even without reaching the code tha tneeds to be commented out.
Comment 2 Jonathan Pryor 2014-01-21 16:09:00 UTC
I believe that the NullPointerException is due to invalid use of Switch. As per the Switch documentation:

http://developer.android.com/reference/android/widget/Switch.html#Switch(android.content.Context,%20android.util.AttributeSet,%20int)
> 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:

http://developer.android.com/training/custom-views/create-view.html

Rephrased, your attrs.xml:

  <declare-styleable name="MyCustomView">
    <attr name="s1" format="reference" />
    <attr name="s2" format="reference" />    
  </declare-styleable>

makes it possible to do this in your layout.axml:

  <experiment13.MyCustomView
    xmlns:custom="http://schemas.android.com/apk/res/Experiment13.Experiment13"
    custom:s1="@string/whatever"
  />

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