cancel
Showing results for 
Search instead for 
Did you mean: 

getView() is not working in controller

Former Member
0 Kudos

Hi experts,

here i am working on camera functionality.

i have image control in view and button, when i click on button image should place in image control.

for this functionality i listed below. but here i am facing the issue with var oView = this.getView();

When i running the application it is showing "Uncaught TypeError: Cannot read property 'getView' of null".

if var oView = this.getView(); is outside of the onPhotoDataSuccess call back function, oView is getting instance of the view, but it is not getting in call back function of onPhotoDataSuccess.

can any one suggest how can i get view reference in  onPhotoDataSuccess function.

and tell me any other alternate solution for this.

Detail.view.xml:

<Image id="myImage" width="200px" height="200px" />

<Button text="get photo" press="capturePhoto" />

Detail.controller.js:

capturePhoto : function() { 

    var oNav = navigator.camera; 

    oNav.getPicture(this.onPhotoDataSuccess, this.onFail, { quality: 10, destinationType: oNav.DestinationType.DATA_URL });

    this._getDialog1().close(); 

    }

onPhotoDataSuccess :  function(imageData) {

        var oView = this.getView();  // error in this line

        var img = oView.byId("myImage");

        img.setSrc("data:image/jpeg;base64," + imageData);

        MessageToast.show("Photo have been Added Successfully"); 

    }

Accepted Solutions (0)

Answers (2)

Answers (2)

junwu
Active Contributor
0 Kudos

put this.onPhotoDataSuccess.bind(this) in capturePhoto

saivellanki
Active Contributor
0 Kudos

Hi Rangaswamy,

Try something like below:


capturePhoto : function() {

    var oNav = navigator.camera;

    oNav.getPicture($.proxy(this.onPhotoDataSuccess, this), $.proxy(this.onFail, this), { quality: 10, destinationType: oNav.DestinationType.DATA_URL });

    this._getDialog1().close();

    }

Regards,

Sai.