I'm trying out the sample HelloWorld application using the ArcGIS with Android SDK. The code compiles fine but there's a grey screen on execution. I've taken a look at this question but it doesn't solve my issue.
Here's my source code:
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();
}
}
Here's my 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)
You cannot check the tileLayer isInitialized return value right away like that. The layer initialization makes network calls and is done asynchronously.
You need to add it to your MapView first, just like in the original HelloWorld sample code. This will start to initialize the layer. Then you can check the events fired from your OnStatusChangedListener for the layer and/or map initializing.