简体   繁体   中英

Cannot read property 'addChild' of undefined

I'm trying to add a dynamic label via a tap event. But get an error that the function is not recognized. In nested containers I have the problem that I can not find a specific reference via getViewById. Get only undefinded out. Am I using getviewbyid incorrectly? With normal containers, the reference gets without problems. Only if these are often nested there are problems. Here is a link where is it working. Tap

XML

<page xmlns="http://schemas.nativescript.org/tns.xsd" class="page" loaded="pageLoaded" 
    xmlns:dd="nativescript-drop-down" 
    xmlns:sd="nativescript-ui-sidedrawer" 
    xmlns:customControls="shared">
    <ActionBar>
        <label style="color:grey;" text="ZorrasBistro" class="mka actionlabel"/>
        <Android>
            <NavigationButton tap="onBackButtonTap" android.systemIcon="ic_menu_back" />
            <label text="&#xf067;" tap="{{newItem}}" class="fas" horizontalAlignment="right" style="margin-right:30em;"/>
        </Android>
        <iOS>
            <ActionItem id="exampleMenuButton" ios.position="right" automationText="ExampleMenu">
                <ActionItem.actionView>
                    <label text="&#xf060;" class="fas"/>
                </ActionItem.actionView>
            </ActionItem>
        </iOS>
    </ActionBar>
    <sd:RadSideDrawer id="sideDrawer">
        <sd:RadSideDrawer.drawerContent>
            <customControls:sidedrawer/>
        </sd:RadSideDrawer.drawerContent>
        <sd:RadSideDrawer.mainContent id="main">
            <GridLayout id="example-background" loaded="onBackgroundLoaded">
                <GridLayout id="form-content" class="form-content" rows="auto,*,auto" loaded="onContentLoaded">
                    <StackLayout class="top-border" row="0"/>
                    <ScrollView row="1" class="fields-section">
                        <StackLayout>
                            <!-- Bestellungname -->
                            <GridLayout ios:columns="auto,*" android:rows="auto,auto" verticalAlignment="top">
                                <Label text="BestellungName" class="field-title" />
                                <TextField id="ordername" text="{{ Ordername }}" class="field" ios:col="1" android:row="1" tap="onTextInputTapped" />
                            </GridLayout>
                            <StackLayout class="line"/>
                            <!-- Name -->
                            <GridLayout ios:columns="auto,*" android:rows="auto,auto" verticalAlignment="top">
                                <Label text="Optional" class="field-title" />
                                <TextField id="email" text="{{ email }}" class="field" ios:col="1" android:row="1" tap="onTextInputTapped" />
                            </GridLayout>
                            <StackLayout class="line"/>
                            <!-- Weiteres -->
                            <GridLayout ios:columns="auto,*" android:rows="auto,auto" verticalAlignment="top">
                                <Label text="Trinken" class="field-title" />
                                <TextField id="password" text="{{ password }}" class="field" ios:col="1" android:row="1" tap="onTextInputTapped" />
                            </GridLayout>
                            <StackLayout class="line"/>
                            <!-- ListPicker -->
                            <GridLayout android:rows="auto,auto" ios:columns="auto,*" verticalAlignment="top">
                                <StackLayout android:row="0" ios:col="0" orientation="horizontal">
                                    <label text="Trinken" class="field-title" />
                                    <label text="Anzahl" style="margin-left:100em;" class = "field-title"/>
                                </StackLayout>
                                <StackLayout android:row="1" ios:col="0" orientation="horizontal">
                                    <ListPicker class="ListPicker" items="{{items}}" textField ="name" valueField="role" selectedIndex ="{{index}}" valueField ="id" selectedValue="{{theid}}">
                                    </ListPicker>
                                    <ListPicker class="ListPicker" items="{{items}}" textField="id" valueField="role" />
                                </StackLayout>
                            </GridLayout>
                            <GridLayout android:rows="auto,auto" ios:columns="auto,*" verticalAlignment="top">
                                <StackLayout id="labeldyn" android:row="0" ios:columns="auto,*" verticalAlignment="top">
                                </StackLayout>
                                <StackLayout id="listpickerdyn" android:row="0" ios:columns="auto,*" verticalAlignment="top">
                                </StackLayout>
                            </GridLayout>
                            <GridLayout android:rows="auto,auto" ios:columns="auto,*"  verticalAlignment = "top">
                                <StackLayout row = "0" orientation="horizontal">
                                    <label text="Mittagessen" class="field-title" />
                                    <label text="Anzahl" style="margin-left:75em;" class="field-title"/>
                                </StackLayout>
                                <StackLayout row="1" orientation="horizontal">
                                    <ListPicker class="ListPicker" items="{{Menu}}" textField="Menu" valueField="role"/>
                                    <ListPicker class="ListPicker" items="{{Anzahl}}" valueField="role" />
                                </StackLayout>
                            </GridLayout>
                            <GridLayout android:rows="auto,auto" ios:columns="auto,*"  verticalAlignment = "top">
                                <StackLayout row="0" orientation="horizontal">
                                    <label text="Mittagessen" class="field-title"/>
                                    <label text="Anzahl" style="margin-left:75em;" class="field-title"/>
                                </StackLayout>
                                <StackLayout row ="1" orientation="horizontal">
                                    <ListPicker class="ListPicker" items="{{Menu}}" textField="Menu" valueField="role"/>
                                    <ListPicker class="ListPicker" items="{{Anzahl}}" valueField="role" />
                                </StackLayout>
                            </GridLayout>
                        </StackLayout>
                    </ScrollView>
                    <Button text="Bestellen" class= "btn btn-active btn-rounded-lg" tap="neueBestell" row="2" />
                </GridLayout>
                <!-- Picture -->
                <Image id="image" src="~/res/food.png" class="profile-picture" tap="onProfilePictureTapped"/>
            </GridLayout>
        </sd:RadSideDrawer.mainContent>
    </sd:RadSideDrawer>
</page>

JS

ViewModel.newItem = (args) => {

        const page = args.object;
        var label = page.getViewById("labeldyn");
        var _label = new Label();
        _label.Text = "Test";
        console.log("Test Ausgabe:" + _label.Text);
        label.addChild(_label);


    };

Output/ Failure

TypeError: Cannot read property 'addChild' of undefined
File: "file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/gestures/gestures.js, line: 228, column: 40

StackTrace:
        Frame: function:'ViewModel.newItem', file:'file:///data/data/org.nativescript.preview/files/app/newBestellung/newBestellung-view.js', line: 33, column: 15
        Frame: function:'_executeCallback', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/gestures/gestures.js', line: 282, column: 27
        Frame: function:'TapAndDoubleTapGestureListenerImpl.onSingleTapUp', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/gestures/gestures.js', line: 25, column: 17
        Frame: function:'GesturesObserver.androidOnTouchEvent', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/gestures/gestures.js', line: 228, column: 41
        Frame: function:'', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 259, column: 25
        Frame: function:'View.handleGestureTouch', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 258, column: 18
        Frame: function:'TouchListenerImpl.onTouch', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 33, column: 19


        at com.tns.Runtime.callJSMethodNative(Native Method)
        at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1203)
        at com.tns.Runtime.callJSMethodImpl(Runtime.java:1083)
        at com.tns.Runtime.callJSMethod(Runtime.java:1070)
        at com.tns.Runtime.callJSMethod(Runtime.java:1050)
        at com.tns.Runtime.callJSMethod(Runtime.java:1042)
        at com.tns.gen.java.lang.Object_view_24_32_TouchListenerImpl.onTouch(Object_view_24_32_TouchListenerImpl.java:19)
        at android.view.View.dispatchTouchEvent(View.java:12625)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3172)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2857)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3172)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2857)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3172)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2857)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3172)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2857)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3172)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2857)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3172)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2857)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3172)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2857)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3172)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2857)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:615)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1879)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3441)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:577)
        at android.view.View.dispatchPointerEvent(View.java:12877)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5787)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5582)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5075)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5128)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5094)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5231)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5102)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5288)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5075)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5128)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5094)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5102)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5075)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7887)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7827)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7788)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7998)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:187)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7961)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:8025)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:652)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:7000)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

Try

const page = args.object.page;

instead of

const page = args.object;

args.object will be the label itself not the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM