有关未定义或空引用的JavaScript运行时错误

时间:2013-06-28 17:11:20

标签: javascript windows-8 windows-runtime winjs appsettings

我已经实现了一个SettingsFlyout。从这个弹出窗口的角度来看,我的应用程序从用户(firsname)收集了一些信息,并希望将其存储在漫游设置中。当用户单击设置视图的按钮并在弹出窗口的beforeShow事件中检索时,将存储此信息。这两个事件是在SettingsFlyout本身的ready函数中设置的,但由于某种原因,我遇到了以下错误。

0x800a138f - JavaScript运行时错误:无法获取未定义或空引用的属性'winControl'

在下一行

var divtest = document.getElementById"test").winControl;

同样我也得到了

0x800a138f - JavaScript运行时错误:无法设置未定义或空引用的属性“onclick”。

你看到我做错了导致这些问题吗?

以下是我在default.html中的内容

app.onsettings = function (e) {

        e.detail.applicationcommands = {
            "test": {
                href: "/pages/settings/test/test.html",
                title: "Test"
            }
        }

        WinJS.UI.SettingsFlyout.populateSettings(e);
    };

这是test.html本身。

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link href="/pages/settings/test/test.css" rel="stylesheet" />

    <script src="/pages/settings/test/test.js"></script>

    </head>
    <body>
        <div 
            data-win-control="WinJS.UI.SettingsFlyout"  data-win-options="{settingsCommandId:'test',  width:'narrow'}">
            <div class="win-header">       
                <div class="win-label">test</div>
            </div>
            <div class="win-content">
                First Name: <input id="firstname" />
                <br />
                <input type="submit" value="Save" />
            </div>
        </div>  
    </body>
</html>

这是test.js文件。

(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/settings/test/test.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.
            var roamingSettings = Windows.Storage.ApplicationData.current.roamingSettings;
            var divtest = document.getElementById("test").winControl;
            var firstname = document.getElementById("firstname");

            document.getElementById("submit").onclick = function (e) {
                //alert('hi');
                roamingSettings.values["firstname"] = firstname.value;
            }

            divtest.addEventListener("beforeshow", function () {
                firstname.value = roamingSettings.values["firstname"];
            });
        },

        unload: function () {
            // TODO: Respond to navigations away from this page.
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        }
    });
})();

2 个答案:

答案 0 :(得分:1)

submit中没有ID testHTML的元素。

答案 1 :(得分:1)

问题可能是因为:

var divtest = document.getElementById("test").winControl;

查找带有Id=test的HTML元素,似乎您设置了

settingsCommandId:'test'

但它不一样,所以应该是:

<div id="test" data-win-control="WinJS.UI.SettingsFlyout"  data-win-options="{settingsCommandId:'test',  width:'narrow'}">