Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
New Contributor
Posts: 9
Registered: ‎11-28-2013
My Device: Z10
My Carrier: vodafone

Application crashing on creating a new object instance of ComponentDefinition.

I am creating a new object instance of a ComponentDefinition using createObject() and then adding it to a container. My application crashes when it reaches the said line of code in QML. Here is my code:

 

import bb.cascades 1.0
import Data.UpdateReservationView 1.0    //a custom C++ qmlRegisterType

Page {
    property alias resDetails: updateRes.resDetails

    attachedObjects: [
            ComponentDefinition {
                id: enhLabel
                Label {
                    textStyle.fontSize: FontSize.PointValue
                    textStyle.fontSizeValue: 5.5
                    textStyle.color: Color.Black
                }	 
            },
            
            UpdateReservationView {
                id: updateRes 
            }
     ]

     onResDetailsChanged: {
         // some code
      
         var newLabel = enhLabel.createObject();  //the app crashes upon reaching this line
         newLabel.text = resDetails[i]["roomName"];
         labelsContainer.add(newLabel);
     }

      ScrollView {
                topMargin: 30.0
                horizontalAlignment: HorizontalAlignment.Fill
           		Container{
		            leftPadding: 20.0
		            rightPadding: leftPadding
		            bottomPadding: 50.0
		            horizontalAlignment: HorizontalAlignment.Fill
		            Container { 
                                 id: labelsContainer
                                 horizontalAlignment: HorizontalAlignment.Left
                         	    verticalAlignment: VerticalAlignment.Center
                            }
                        }
      } //ScrollView ends
} //Page ends

 I have tried using .load() instead of .createObject() but get the same result as the ComponentDefinition (enhLabel) is being instantiated elswhere in the code too, and according to my understanding .load() simply loads the control once whereas I require a new instance of it to created multiple time dynamically. 

The queer thing is that the code was running perfectly the day before and I've made no changes in this particular QML file. 

What could be the reason for the application to crash. I am sure that it crashes upon reaching the var newLabel = enhLabel.createObject() line by using the old school console.debug() method. 

Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Application crashing on creating a new object instance of ComponentDefinition.

Hi!
Could you try replacing this line:
newLabel.text = resDetails[i]["roomName"];
with
newLabel.text = "Test"
and see if anything changes?

Andrey Fidrya, @zmeyc on twitter
New Contributor
Posts: 9
Registered: ‎11-28-2013
My Device: Z10
My Carrier: vodafone

Re: Application crashing on creating a new object instance of ComponentDefinition.

Tried your suggestion. The application is still crashing.

Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: Application crashing on creating a new object instance of ComponentDefinition.

your component definition looks wrong. try adding the keyword content, like this

 


HumptyDumptyEIZ wrote:

I am creating a new object instance of a ComponentDefinition using createObject() and then adding it to a container. My application crashes when it reaches the said line of code in QML. Here is my code:

 

    attachedObjects: [
            ComponentDefinition {
                id: enhLabel
                content: Label {
                    textStyle.fontSize: FontSize.PointValue
                    textStyle.fontSizeValue: 5.5
                    textStyle.color: Color.Black
                }	 
            },
            
            UpdateReservationView {
                id: updateRes 
            }
     ]

 I have tried using .load() instead of .createObject() but get the same result as the ComponentDefinition (enhLabel) is being instantiated elswhere in the code too, and according to my understanding .load() simply loads the control once whereas I require a new instance of it to created multiple time dynamically. 

The queer thing is that the code was running perfectly the day before and I've made no changes in this particular QML file. 

What could be the reason for the application to crash. I am sure that it crashes upon reaching the var newLabel = enhLabel.createObject() line by using the old school console.debug() method. 




-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
New Contributor
Posts: 9
Registered: ‎11-28-2013
My Device: Z10
My Carrier: vodafone

Re: Application crashing on creating a new object instance of ComponentDefinition.

Tried the new ComponentDefinition as per your code. The app's still crashing.

BlackBerry Development Advisor
Posts: 81
Registered: ‎09-09-2013
My Device: BlackBerry Z10
My Carrier: Etisalat UAE

Re: Application crashing on creating a new object instance of ComponentDefinition.

Hello -

 

I've tweaked your code snippet as shown below as it was missing the custom page "UpdateReservationView". and the code is working properly and the ComponentDefinition created.

 

Would you share the entire pieces of the code so we can inspect the issue?

 

Page {
    property string resDetails: "K"
    
    attachedObjects: [
        ComponentDefinition {
            id: enhLabel
            Label {
                textStyle.fontSize: FontSize.PointValue
                textStyle.fontSizeValue: 5.5
                textStyle.color: Color.Black
            }	 
        }
    ]
    
    onResDetailsChanged: {
        var newLabel = enhLabel.createObject();  //the app crashes upon reaching this line
        newLabel.text = "Label Just Created";
        labelsContainer.add(newLabel);
    }
    ScrollView {
        topMargin: 30.0
        horizontalAlignment: HorizontalAlignment.Fill
        Container{
            leftPadding: 20.0
            rightPadding: leftPadding
            bottomPadding: 50.0
            horizontalAlignment: HorizontalAlignment.Fill
            Button {
                onClicked: {
                    resDetails = "A";
                }
            	text: "Change Value"
            }
            Container { 
                id: labelsContainer
                horizontalAlignment: HorizontalAlignment.Left
                verticalAlignment: VerticalAlignment.Center
            }
        }
    } //ScrollView ends
} //Page ends

 

Kareem ELSayed | Application Development Consultant | Developer Relations | BlackBerry | Twitter:@kemobyte