Android:在 Asynctask 上创建 MapView 不起作用

发布于 2024-12-01 20:20:55 字数 833 浏览 1 评论 0原文

我正在尝试在另一个线程上创建 MapView,因为加载 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
    }
}

程序在“ThreadPoolExecutor.class”处停止:

} finally {
    processWorkerExit(w, completedAbruptly);
}

注意:我确实知道每个进程 1 个实例的 MapActivity/MapView 限制。在执行此 AsyncTask 之前我尚未创建 MapView 对象。

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 per process. I haven't created a MapView object prior to executing this AsyncTask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倥絔 2024-12-08 20:20:55

我从 XML 文件扩充地图,然后将其推送到布局容器。

公共无效运行()
{
尝试{

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

和地图 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"
    />

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"
    />
掩耳倾听 2024-12-08 20:20:55

首先,我认为您至少需要获取地图的调试密钥,否则您只会得到一个空白屏幕

,然后如果您阅读有关构造函数的信息,

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.**

地图必须扩展 MapActivity。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文