【问题标题】:error :MapViews can only be created inside instances of MapActivity错误:MapViews 只能在 MapActivity 的实例中创建
【发布时间】:2014-04-23 19:57:17
【问题描述】:

我使用 Eclipse 和 Android SDK,并且我有一个类不要将每个代码都放在主要活动中:

blablabla
import blablabla
..
..
public class Mhelper {
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
      static final LatLng KIEL = new LatLng(53.551, 9.993);
      private GoogleMap map;
      Context mContext;

    public Mhelper(Context ctx) {
          this.mContext= ctx;
    }

    public void pinpointlocation(Context context){

         map = ((SupportMapFragment) ((FragmentActivity) mContext).getSupportFragmentManager().findFragmentById(R.id.mapview))
                .getMap();
                Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                    .title("Hamburg"));
                Marker kiel = map.addMarker(new MarkerOptions()
                    .position(KIEL)
                    .title("Kiel")
                    .snippet("Kiel is cool")
                    .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));

                // Move the camera instantly to hamburg with a zoom of 15.
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

                // Zoom in, animating the camera.
                map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }
....
....
....

我在 xml 中放了一个视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

...
...
...
...


<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="KEY"
/>

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/button2" />

</RelativeLayout>

程序编译和安装没有任何错误,但是当我在模拟器中运行应用程序时,我得到:

blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hach/com.example.hach.MainActivity}: android.view.InflateException: Binary XML file line #48: Error inflating class com.google.android.maps.MapView
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): Caused by: android.view.InflateException: Binary XML file line #48: Error inflating class com.google.android.maps.MapView
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.

我在堆栈中搜索并找到thisthisthis

第一个只是承认他不应该使用 viewmap,第二个告诉他阅读这个refrence 这是一个很好的但我读了它并且找不到任何我可以在我的项目中应用的解决方案。第三个说 Google Maps Android API v2 不错。

我找到了this 链接,这是一个很好的例子,但我不想使用制表符,是吗?除了线

import com.google.android.maps.MapActivity;

是我创建新类并想扩展 MapActivity 时的问题,IDE 不知道 MapActivity

在不完全更改应用程序的情况下,我可以对当前项目做些什么来显示地图并在我编码时指向一个位置?我应该扩展 MapActivity 并将其添加到项目中吗?有没有像 mapview 这样的简单解决方案?

【问题讨论】:

  • 就像错误说你不能那样做。为什么不在您的活动中创建地图对象,然后将其传递给您的助手类
  • 不要使用com.google.android.maps.MapActivity,这是 v1 中已弃用的地图活动
  • 是的,我做到了,但同样的错误,但这次来自 mainactivity。
  • @tyczj 我删除了 com.google.android.maps.MapActivity,只是测试。
  • 您似乎在混合和匹配 Maps V1 和 Maps V2,这不起作用。由于地图 V1 不再适用于新应用,因此请关注 Maps V2 and its documentation

标签: java android eclipse google-maps android-fragments


【解决方案1】:

我想通了。我首先删除地图视图(CommonsWare 是对的,它在 V1 中)然后我添加了

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

manifest.然后它工作。这个问题是由一些不合格的网站引起的,他们试图在没有测试的情况下放一个教程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多