【问题标题】:Unfortunately, your application stopped working不幸的是,您的应用程序停止工作
【发布时间】:2013-03-26 18:58:19
【问题描述】:

在 Alloy 框架中,我想动态添加一个在 xml 文件中定义但不绑定到任何其他视图的视图。

我们来看下面的例子:

容器我要填写index.xml:

<ScrollableView id="scrollableBilan" showPagingControl="true">
</ScrollableView>

我想为进入 ScrollableView 的每个视图实例化的视图模板 question.xml:

<Alloy>
    <Collection src="ReponsePossible">
    <View id="questionContainer" class="container">
        <Label id="questionText" />
        <Button id="buttonNextQuestion">Question suivante</Button>
    </View>
</Alloy>

最后是控制器 index.js,问题是 Collection 实例:

for(var i=0; i<questions.length; i++){
        $.scrollableBilan.add(Alloy.createController('question', questions.at(i)));
    }

这使我的应用程序崩溃并显示以下消息:“不幸的是,您的应用程序已停止”。我已经收到了这个错误,总是在尝试使用 Alloy.createController 动态添加视图时。

使用 Ti.UI.createView 创建视图时行为正常,但我想使用 MVC...

欢迎任何帮助!

【问题讨论】:

    标签: view titanium appcelerator titanium-alloy


    【解决方案1】:

    您正在向它传递一个模型对象,而不是尝试向它传递一个 JSON 对象,如下所示:

    for(var i=0; i<questions.length; i++){
        $.scrollableBilan.add(Alloy.createController('question', questions.at(i).toJSON()));
    }
    

    或者,您可以使用Data BindingdataCollection 属性在xml 文件中执行所有这些操作,在您的index.xml 中添加类似这样的内容:

    <Alloy>
        <Collection src="questions">
        <ScrollableView id="scrollableBilan" showPagingControl="true" dataCollection="ReponsePossible">
            <View id="questionContainer" class="container">
                <Label id="questionText" text="{questionText}"/>
                <Button id="buttonNextQuestion">Question suivante</Button>
            </View> 
        </ScrollableView>
    </Alloy>
    

    questionText 字段必须是 questions 模型中的属性。

    【讨论】:

    • 再次感谢乔赛亚。第一个解决方案正在工作。但是,我不喜欢纯钛 ui 元素创建和合金之间的这种混合类型。我更喜欢第二种解决方案,但此 dataCollection 绑定功能仅适用于下一版本的 Appcelerator SDK (jira.appcelerator.org/browse/ALOY-451)。我将等到 4 月 15 日以这种方式实施。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    相关资源
    最近更新 更多