无法读取未定义的属性“ addChild”

时间:2019-04-10 06:35:50

标签: javascript android nativescript

我正在尝试通过点击事件添加动态标签。但是会收到一个错误,指出该功能无法识别。在嵌套容器中,我有一个问题,我无法通过getViewById找到特定的引用。只得到未发现的。我是否正确使用了getviewbyid?对于普通容器,引用不会出现问题。只有当这些嵌套在一起时,才有问题。 这是一个在哪里工作的链接。 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);


    };

输出/故障

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)

1 个答案:

答案 0 :(得分:0)

尝试

const page = args.object.page;

代替

const page = args.object;

args.object将是标签本身而不是页面。