开发者

Android: Create MapView on Asynctask doesn't work

I'm trying to create a MapView on another thread since it takes too long to load an Activity.

class MapCreation extends AsyncTask<Integer, Void, MapView>
{
    MapActivity context;

    public MapCreation(MapActivity context)
    {
        this.context = context;
    }

    @Override
    protected MapView doInBackground(Integer... params)
    {
        ListView someListView = new ListView(context); //Completely fine!
        MapView someMapView = new MapView(context, OMITTED_KEY); //!!!!CRASH!!!!
        return someMapView;
    }

    protected void onPostExecute(MapView someMapView)
    {
           //do something
    }
}

The program stops at "ThreadPoolExecutor.class" at:

} finally {
    processWorkerExit(w, completedAbruptly);
}

Note: I do know about the MapActivity/MapView limit of 1 instance p开发者_开发百科er process. I haven't created a MapView object prior to executing this AsyncTask.


I inflate the map from an XML file and than push it to a layout container.

public void run() { try {

                    MapsInitializer.initialize(activityHost);

                    LayoutInflater inflater = (LayoutInflater) activityHost.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    mapView = (MapView) inflater.inflate(R.layout.map, mapView, true);

                    mapContainer.addView(mapView);
                    mapView.onCreate(null);
                    mapView.onResume();

                    googleMap = mapView.getMap();
                    if (googleMap == null)
                        return;

                    googleMap.setMyLocationEnabled(false);
                    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mapLocation, 15.0f));
                    googleMap.getUiSettings().setZoomControlsEnabled(false);
                    googleMap.getUiSettings().setAllGesturesEnabled(false);


                } catch (GooglePlayServicesNotAvailableException e) {
                    Log.e("ERROR", "ERROR -  failed to create map");
                    return;
                }
            }
        }

and the map xml:

<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/some_id"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:apiKey="YOUR_ID"
    android:visibility="visible"
    />


First I think you need at least to get the debug key for the map otherwise you will just get a blank screen

then if you read info about constructor

public MapView(android.content.Context context,
           java.lang.String apiKey)

Constructs a MapView object.

Parameters:
    context - A MapActivity object.
    apiKey - A Google Maps API Key. See Obtaining a Maps API Key for complete information. 
Throws:
    java.lang.IllegalArgumentException - **if the enclosing context is not an instance of MapActivity.**

The map has to extend the MapActivity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜