Bug: Crash in initialize on Drawables subclass defined in a layout

Former Member
Former Member
Steps to reproduce:
  • Define a subclass of Drawable
    class GraphDrawable extends Drawable {

    function initialize(params) {
    Drawable.initialize(params);
    }

    function draw(dc) { ... }
    }

  • Define a layout that references the custom Drawable
    <layout id="MainLayout" ...>
    <drawable id="my_id" class"GraphDrawable" />
    </layout>

  • Set layout in your View
    class GraphDataField extends Ui.DataField {
    function onLayout(dc) {
    setLayout(Rez.Layouts.MainLayout(dc));
    }
    }



Expected behavior: The class is initialized without error
Observed behavior: App (a data field) crashes with error:

Unexpected Type Error
in initialize (/Users/charpentier/garmin/git/toolchain/mbsimulator/submodules/technology/monkeybrains/virtual-machine/api/WatchUi.mb:1269)


Workaround:
Define at least one param element inside the drawable definition:
<layout id="MainLayout" ...>
<drawable id="my_id" class"GraphDrawable">
<param name="locX">0</param>
<param name="locY">0</param>
</drawable>
</layout>
  • Is it that you need to define just one property, or is it that the locX and locY parameters are not optional?
  • Former Member
    Former Member over 9 years ago
    Is it that you need to define just one property, or is it that the locX and locY parameters are not optional?


    The former, you need to specify at least one param, no matter if it's known by Drawable's initialize method.

    After further investigation it looks like if you don't define any params in the XML, the injected dictionary is null. But Drawable.initialize does not accept null as input!
    Thus, another workaround would be to prevent passing null:
    function initialize(params) {
    if (params == null) {
    params = {};
    }
    Drawable.initialize(params);
    }


    However, since Drawable.initialize requires a non-null parameter dictionary, the resource loader should not pass null here.