createView method on TiViewProxy is being call too late

You must Login before you can answer or comment on any questions.

Hi, im not sure if that is the best title, but anyway.

Im using an android module to create my own android widget. My problem is that when I try to do a modification on my view, by calling a method that i have in my proxy, the TiUIView hasnt been created yet (the createView method on my proxy hasnt been called yet), so when i do view.someMethod I got a NullPointerException. I also have another proxy method that its been called when I tap any part of my widget, and it works, because for the time i touch the screen the TiUIView has been already created.

This is my javascript code that Im using for instantiating and calling my proxy

function Chart() {
    //create object instance, a parasitic subclass of Observable
    var chartModule = require('com.fede.chartmodule');
    var events = [0.1, 0.375, 0.35, 0.625, 0.7, 0.875, 0.8, 0.125, 0.91, 0.375];
    var chart = chartModule.createChartView();
    chart.addEventListener("click", function(e) {
        chart.setSelection(e.x);
    });
    chart.setEvents(events);
}
module.exports = Chart;
And this is my Proxy code
@Kroll.proxy(creatableInModule=ChartmoduleModule.class)
public class ChartViewProxy extends TiViewProxy
{
    public ChartViewProxy()
    {
        super();
    }
 
    @Override
    public TiUIView createView(Activity activity)
    {
        view = new ChartView(this);
        view.getLayoutParams().autoFillsHeight = true;
        view.getLayoutParams().autoFillsWidth = true;
        return view;
    }
.
.
.
    public void setSelection(Float x)
    {
        view.setSelection(x);
    }
 
    public void setEvents(Float[] events)
    {
        ArrayList<float[]> eventsPoints = new ArrayList<float[]>();
        for(int i = 0; i < events.length; i += 2) {
            float[] point = {events[i], events[i + 1]};
            eventsPoints.add(point);
        }
 
        view.setEventsPoints(eventsPoints);
    }
}
So, I got a NullPointerException when calling setEvents(...) but there is no problem when calling setSelection(...)

Also, I "solved" this problem by instantiating the view on the setEvents(...) method if its null. I just dont think this is a correct way to solve this problem and I would like to know whats happening here!!!

Thanks for your help guys!!!

1 Answer

You may want to make view to a class var so its accessable through this on the class level ?

— answered 12 months ago by Alexander Bauer
answer permalink
1 Comment
  • Im doing it that way, its just that i didnt copy that part of the code. Actualy I made some other mistakes copying the code. This is my current code now.

    @Kroll.proxy(creatableInModule=ChartmoduleModule.class)
    public class ChartViewProxy extends TiViewProxy
    {
        // Standard Debugging variables
        private static final String LCAT = "ExampleProxy";
        private static final boolean DBG = TiConfig.LOGD;
        private ChartView view;
     
        // Constructor
        public ChartViewProxy()
        {
            super();
        }
     
        @Override
        public TiUIView createView(Activity activity)
        {
            if(view == null) {
                view = new ChartView(this);
                view.getLayoutParams().autoFillsHeight = true;
                view.getLayoutParams().autoFillsWidth = true;
            }
            return view;
        }
     
        // Handle creation options
        @Override
        public void handleCreationDict(KrollDict options)
        {
            super.handleCreationDict(options);
     
            if (options.containsKey("message")) {
                Log.d(LCAT, "example created with message: " + options.get("message"));
            }
        }
     
        // Methods
        @Kroll.method
        public void printMessage(String message)
        {
            Log.d(LCAT, "printing message: " + message);
        }
     
     
        @Kroll.getProperty @Kroll.method
        public String getMessage()
        {
            return "Hello World from my module";
        }
     
        @Kroll.setProperty @Kroll.method
        public void setMessage(String message)
        {
            Log.d(LCAT, "Tried setting module message to: " + message);
        }
     
        @Kroll.method
        public int setSelection(Float x, Float y)
        {
            return view.setSelection(x, y);
        }
     
        @Kroll.setProperty @Kroll.method
        public void setEvents(Float[] events)
        {
            ArrayList<float[]> eventsPoints = new ArrayList<float[]>();
            for(int i = 0; i < events.length; i += 2) {
                float[] point = {events[i], events[i + 1]};
                eventsPoints.add(point);
            }
            createView(this.getActivity());
            view.setEventsPoints(eventsPoints);
        }
    }
    As you can see Im calling
    createView(Activity activity)
    from
    setEvents(Float[] events)
    as is the easiest way i found to "solve" this problem.

    I also tried to call it from the constructor, but that throws an exception.

    Thanks!!

    — commented 12 months ago by Federico Suarez Demaestri

Your Answer

Think you can help? Login to answer this question!