0

我正在尝试使用带有 Android SDK 的 ArcGIS 的示例 HelloWorld 应用程序。代码编译良好,但执行时出现灰屏。我已经看了一下这个问题,但它并没有解决我的问题。

这是我的源代码:

public class HelloWorld extends Activity {

    MapView map = null;
    ArcGISTiledMapServiceLayer tileLayer;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        map = (MapView) findViewById(R.id.map);
        tileLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Shaded_Relief/MapServer");
        if(tileLayer.isInitialized() == true)  //if a bad url is provided, this will fail
        {   map.addLayer(tileLayer);  }
        else{
                //if no layer is successfully added, the MapView will not initialize
            Toast layerToast = Toast.makeText(this, "Layer didn't load, MapView won't initialize", Toast.LENGTH_LONG);
            layerToast.show();
        }

        map.setOnStatusChangedListener(new OnStatusChangedListener() {
           private static final long serialVersionUID = 1L;

           public void onStatusChanged(Object source, STATUS status) {
               //conditional checks if mapView's status has changed to initialized 
                if (OnStatusChangedListener.STATUS.INITIALIZED == status && source == map) 
                { 
                    Toast mapViewToast = Toast.makeText(HelloWorld.this, "MapView loaded", Toast.LENGTH_LONG);
                    mapViewToast.show();
                }
            }
         });

    }

    @Override
    protected void onPause() {
        super.onPause();
        map.pause();
 }
    @Override   protected void onResume() {
        super.onResume(); 
        map.unpause();
    }

}

这是我的 LogCat:

06-18 10:51:21.413: E/ArcGIS(1544): url =http://services.arcgisonline.com/ArcGIS/rest/services/World_Shaded_Relief/MapServer
06-18 10:51:21.413: E/ArcGIS(1544): org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
06-18 10:51:21.413: E/ArcGIS(1544):  at [Source: java.io.StringReader@4057ed68; line: 1, column: 2]
06-18 10:51:21.413: E/ArcGIS(1544):     at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1432)
06-18 10:51:21.413: E/ArcGIS(1544):     at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
06-18 10:51:21.413: E/ArcGIS(1544):     at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
06-18 10:51:21.413: E/ArcGIS(1544):     at org.codehaus.jackson.impl.ReaderBasedParser._handleUnexpectedValue(ReaderBasedParser.java:1198)
06-18 10:51:21.413: E/ArcGIS(1544):     at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:485)
06-18 10:51:21.413: E/ArcGIS(1544):     at com.esri.core.internal.b.a.e.a(Unknown Source)
06-18 10:51:21.413: E/ArcGIS(1544):     at com.esri.core.internal.b.a.e.a(Unknown Source)
06-18 10:51:21.413: E/ArcGIS(1544):     at com.esri.core.internal.b.a.e.a(Unknown Source)
06-18 10:51:21.413: E/ArcGIS(1544):     at com.esri.core.internal.a.a.m.b(Unknown Source)
06-18 10:51:21.413: E/ArcGIS(1544):     at com.esri.android.map.ags.ArcGISTiledMapServiceLayer.initLayer(Unknown Source)
06-18 10:51:21.413: E/ArcGIS(1544):     at com.esri.android.map.ags.ArcGISTiledMapServiceLayer$1.run(Unknown Source)
06-18 10:51:21.413: E/ArcGIS(1544):     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:444)
06-18 10:51:21.413: E/ArcGIS(1544):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-18 10:51:21.413: E/ArcGIS(1544):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-18 10:51:21.413: E/ArcGIS(1544):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-18 10:51:21.413: E/ArcGIS(1544):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-18 10:51:21.413: E/ArcGIS(1544):     at java.lang.Thread.run(Thread.java:1019)
4

2 回答 2

2

您不能像那样立即检查 tileLayer isInitialized 返回值。层初始化进行网络调用并异步完成。

您需要先将其添加到您的 MapView 中,就像在原始的 HelloWorld 示例代码中一样。这将开始初始化图层。然后,您可以检查从 OnStatusChangedListener 触发的事件以进行图层和/或地图初始化。

于 2012-06-18T17:17:40.220 回答
0

如果您因为特定原因不需要检查层初始化,则不使用 isInitialized() 将其添加到 MapView。

如果您确实需要在继续之前检查图层是否已初始化,那么您可以使用 OnStatusChangedListener,就像 MapView 一样。然后,您可以等到图层的状态更改为 INITIALIZED,然后将其添加到 MapView。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    map = (MapView) findViewById(R.id.map);
    tileLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Shaded_Relief/MapServer");
    tileLayer.setOnStatusChangedListener(new OnStatusChangedListener() {
        public void onStatusChanged(Object source, STATUS status) {
            if (OnStatusChangedListener.STATUS.INITIALIZED == status){
                map.addLayer(tileLayer);  //when layer is initialized add to map
            }
        }
    });

    map.setOnStatusChangedListener(new OnStatusChangedListener() {
       public void onStatusChanged(Object source, STATUS status) {
            if (OnStatusChangedListener.STATUS.INITIALIZED == status ) 
            { /*do something when mapView is initialized*/  }
        }
    });

}
于 2012-06-19T19:15:19.980 回答