JavaScript 运行时错误 : Unable to get property 'removeChild' of undefined or null reference

标签 javascript gridview windows-store-apps bing-maps

我正在为 Windows 8 应用程序使用网格应用程序模板,并在默认模板中的最后一个 View 之后添加了一个新 View 。

这是我推送新 View 的代码:

function ShowOnMap() {
    //"shows" the selected shop on a map
    //TODO use google geocoding because bing maps don't support greek streets (except big ones)
    var splitAddress = address.split(": ");
    nav.navigate("/pages/mapsDetails/bingMaps.html", { address: splitAddress[1] });
}

这是我的新 View 的 html 代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>map</title>

    <!-- WinJS references -->
    <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <script type="text/javascript" src="ms-appx:///Bing.Maps.JavaScript//js/veapicore.js"></script>
    <script type="text/javascript" src="ms-appx:///Bing.Maps.JavaScript//js/veapiModules.js"></script>

    <script src="/pages/mapsDetails/bingMaps.js"></script>
</head>
<body>
    <div class="map fragment">
        <header aria-label="Header content" role="banner">
            <button id="buttonBack" class="win-backbutton" aria-label="Back" disabled type="button"></button>
            <h1 class="titlearea win-type-ellipsis"><span class="pagetitle">Αναπαράσταση στον χάρτη</span></h1>
        </header>
        <section aria-label="Main content" role="main">
            <div id="content" style="display: -ms-grid; -ms-grid-rows: 1fr 1fr; -ms-grid-columns: 100% 0%; height: 90%; overflow: hidden;">
                <div id="leftContainer" style="-ms-grid-column: 1; -ms-grid-row: 1; -ms-grid-row-span: 2;">
                    <div id="myMap" style="position: relative; width: 100%; height:90%; "></div>
                </div>
                <div id="rightContainer" style="-ms-grid-column: 2; -ms-grid-row: 1;"></div>
            </div>
        </section>
    </div>
</body>
</html>

这是我的 JavaScript 代码:

// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
var map, geoLocationProvider, gpsLayer, addressForGeocoding, searchManager;

(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/mapsDetails/bingMaps.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) {
            //i only get the city because greek addresses not supported by bing maps only major ones...s
            //TODO use google geocoding service.
            var city = options.address.split(",");
            addressForGeocoding = city[city.length - 1];
            Microsoft.Maps.loadModule('Microsoft.Maps.Map', { callback: initMap });
        },
    });
})();

function initMap() {
    //initilize a map and a searchManager
    try {
        var mapOptions =
        {
            credentials: "MYKEY",
            zoom: 2
        };

        map = new Microsoft.Maps.Map(document.getElementById("myMap"), mapOptions);

        gpsLayer = new Microsoft.Maps.EntityCollection();
        map.entities.push(gpsLayer);

        if (!searchManager) {
            map.addComponent('searchManager', new Microsoft.Maps.Search.SearchManager(map));
            searchManager = map.getComponent('searchManager');
        }

        if (searchManager) {
            geocodeRequest();
        }
        else {
            Microsoft.Maps.loadModule('Microsoft.Maps.Search', { callback: geocodeRequest });
        }
    }
    catch (e) {
        var md = new Windows.UI.Popups.MessageDialog(e.message);
        md.showAsync();
    }
}

function geocodeRequest() {
    // geocode api request
    // var where = greekToGreeklish(addressForGeocoding); //thats the right one but crashes the app when address is in Greece
    var where = 'Volos';
    var userData = { name: 'Maps Test User', id: '' };
    var request =
    {
         where: where,
         count: 2,
         bounds: map.getBounds(),
         callback: onGeocodeSuccess,
         errorCallback: onGeocodeFailed,
         userData: userData
    };
    searchManager.geocode(request);
}


function onGeocodeSuccess(result, userData){
    // geocode api request success callback
    if (result) {
         map.entities.clear();
         var MM = Microsoft.Maps;
         var topResult = result.results && result.results[0];
         if (topResult){
            var pushpin = new MM.Pushpin(topResult.location, null);
            map.setView({ center: topResult.location, zoom: 10 });
            map.entities.push(pushpin);
         }
    }
}


function onGeocodeFailed(result, userData){
    // geocode api request failure callback
    var md = new Windows.UI.Popups.MessageDialog("Geocode failed");
    md.showAsync();
}

function greekToGreeklish(transormString){
    //convert greek characters to english
    transormString = transormString.replace("ς", "s");
    transormString = transormString.replace("ε", "e");
    transormString = transormString.replace("Ε", "e");
    transormString = transormString.replace("έ", "e");
    transormString = transormString.replace("E", "e");
    transormString = transormString.replace("ρ", "r");
    transormString = transormString.replace("Ρ", "r");
    transormString = transormString.replace("τ", "t");
    transormString = transormString.replace("Τ", "t");
    transormString = transormString.replace("υ", "i");
    transormString = transormString.replace("Y", "u");
    transormString = transormString.replace("ύ", "u");
    transormString = transormString.replace("Ύ", "u");
    transormString = transormString.replace("θ", "th");
    transormString = transormString.replace("Θ", "th");
    transormString = transormString.replace("ι", "i");
    transormString = transormString.replace("É", "e");
    transormString = transormString.replace("Ί", "i");
    transormString = transormString.replace("ί", "i");
    transormString = transormString.replace("ο", "o");
    transormString = transormString.replace("ό", "o");
    transormString = transormString.replace("Ο", "o");
    transormString = transormString.replace("Ο", "o");
    transormString = transormString.replace("π", "p");
    transormString = transormString.replace("Π", "p");
    transormString = transormString.replace("α", "a");
    transormString = transormString.replace("ά", "a");
    transormString = transormString.replace("Α", "a");
    transormString = transormString.replace("Ά", "a");
    transormString = transormString.replace("σ", "s");
    transormString = transormString.replace("Σ", "s");
    transormString = transormString.replace("δ", "d");
    transormString = transormString.replace("Δ", "d");
    transormString = transormString.replace("Φ", "f");
    transormString = transormString.replace("φ", "f");
    transormString = transormString.replace("γ", "g");
    transormString = transormString.replace("Γ", "g");
    transormString = transormString.replace("η", "i");
    transormString = transormString.replace("ή", "i");
    transormString = transormString.replace("H", "i");
    transormString = transormString.replace("Ή", "i");
    transormString = transormString.replace("ξ", "ks");
    transormString = transormString.replace("Ξ", "ks");
    transormString = transormString.replace("κ", "k");
    transormString = transormString.replace("Κ", "k");
    transormString = transormString.replace("λ", "l");
    transormString = transormString.replace("Λ", "l");
    transormString = transormString.replace("ζ", "z");
    transormString = transormString.replace("Ζ", "z");
    transormString = transormString.replace("χ", "ch");
    transormString = transormString.replace("Χ", "CH");
    transormString = transormString.replace("Ψ", "ps");
    transormString = transormString.replace("ψ", "ps");
    transormString = transormString.replace("ω", "o");
    transormString = transormString.replace("ώ", "o");
    transormString = transormString.replace("ω", "o");
    transormString = transormString.replace("ω", "o");
    transormString = transormString.replace("Β", "b");
    transormString = transormString.replace("β", "b");
    transormString = transormString.replace("ν", "n");
    transormString = transormString.replace("N", "n");
    transormString = transormString.replace("μ", "m");
    transormString = transormString.replace("Μ", "m");
    transormString = transormString.replace("Ί", "i");
    transormString = transormString.replace("σ", "s");
    transormString = transormString.replace("ς", "s");
    transormString = transormString.replace("ρ", "r");
    transormString = transormString.replace("Ρ", "r");
    transormString = transormString.replace("ε", "e");
    transormString = transormString.replace("λ", "l");
    transormString = transormString.replace("ς", "s");
    transormString = transormString.replace("ό", "o");
    return transormString;

}

异常

Unhandled exception at line 1, column 204138 in ms-appx://a4614649-5225-4e7c-80f5-362bb99b91ff/Bing.Maps.JavaScript//js/veapicore.js

0x800a138f - JavaScript runtime error: Unable to get property 'removeChild' of undefined or null reference

这是控制台

'WWAHost.exe' (Script): Loaded 'Script Code (MSAppHost/1.0)'. 
Exception was thrown at line 1, column 3756 in ms-appx://a4614649-5225-4e7c-80f5-362bb99b91ff/Bing.Maps.JavaScript//js/veapicore.js
0x800a139e - JavaScript runtime error: SyntaxError
Exception was thrown at line 1, column 3756 in ms-appx://a4614649-5225-4e7c-80f5-362bb99b91ff/Bing.Maps.JavaScript//js/veapicore.js
0x800a139e - JavaScript runtime error: SyntaxError
Exception was thrown at line 1, column 45932 in ms-appx://a4614649-5225-4e7c-80f5-362bb99b91ff/Bing.Maps.JavaScript//js/veapicore.js
0x8000ffff - JavaScript runtime error: Unexpected call to method or property access.
Exception was thrown at line 1, column 3756 in ms-appx://a4614649-5225-4e7c-80f5-362bb99b91ff/Bing.Maps.JavaScript//js/veapicore.js
0x800a139e - JavaScript runtime error: SyntaxError
Exception was thrown at line 1, column 3756 in ms-appx://a4614649-5225-4e7c-80f5-362bb99b91ff/Bing.Maps.JavaScript//js/veapicore.js
0x800a139e - JavaScript runtime error: SyntaxError
Unhandled exception at line 1, column 204138 in ms-appx://a4614649-5225-4e7c-80f5-362bb99b91ff/Bing.Maps.JavaScript//js/veapicore.js
0x800a138f - JavaScript runtime error: Unable to get property 'removeChild' of undefined or null reference
The program '[4060] WWAHost.exe' has exited with code -1 (0xffffffff).

如何修复按下后退按钮时发生的崩溃。它应该可以很好地工作,无需任何代码,我只需推送和弹出 View ...

最佳答案

我遇到了同样的错误。这种情况发生的概率约为 10%。当您收到异常时,它是一条指向 veapicore.js

v.parentNode.removeChild(v); //where parentNode is null which when it causes the problem

然后,当您查看 v 是什么时,您将看到它的 NavBar_ModeSelectorControl,它是 BingMaps 的控制面板。我的解决方法是不使用控制面板,这个错误就消失了。 无需控制面板即可创建 map :

Microsoft.Maps.loadModule('Microsoft.Maps.Map', {
            callback: function () {
                try {
                    var mapDiv = document.getElementById("mapdiv");
                    var mapOptions =
                    {
                        credentials: BING_MAP_KEY,
                        mapTypeId: Microsoft.Maps.MapTypeId.birdseye,
                        showDashboard: false //<--- disables the control panel
                    };
                    BingMap.Map = new Microsoft.Maps.Map(mapDiv, mapOptions);
                }
                //...

我也在等待官方修复此问题。

关于JavaScript 运行时错误 : Unable to get property 'removeChild' of undefined or null reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15185165/

相关文章:

javascript - 尝试替换元素时整个 div 被替换

javascript - 减速时机

javascript - DropzoneJS : Preview selected image before upload in div as <img>

c# - 当某些单元格值为空时对单元格值求和并显示在 asp.net gridview 的页脚中

windows-runtime - 字体系列在 Windows Phone 8.1 中无效

javascript - 创建一个 HTML 邮件编辑器。继承类?

Android 设计自定义 View

c# - 使用 List<> 作为数据源隐藏 DataGridView 中的列?

c# - UWP - 应用内产品 - GetAppLicenseAsync() 的速度

javascript - 如何在 Metro 应用程序中存储用户偏好