JavaScript运行时错误:无法获取未定义或空引用的属性“removeChild”

时间:2013-03-03 11:27:12

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

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

这是推送新视图的代码:

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] });
}

这是我的新视图的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).

当我按下后退按钮时,如何修复崩溃。它应该在没有任何代码的情况下运行良好我只是推送和弹出视图......

1 个答案:

答案 0 :(得分:2)

我得到了同样的错误。它发生在大约10%的时间。当你得到一个Exception时,它会指向一行 veapicore.js

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

然后当你看到什么是v时,你会看到它是NavBar_ModeSelectorControl,它是BingMaps的控制面板。我的工作不是使用控制面板,这个错误就消失了。 创建没有控制面板的地图:

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);
                }
                //...

我也在等待官方解决此问题。