1

我正在尝试在我的模拟器和设备上加载 mapview

我已经尝试了两天,但有时我只是得到没有任何地图的网格线,有时是致命错误,我的应用程序无法在我的模拟器和设备上启动。

我尝试了一切,互联网上存在的每个代码,但没有任何效果。你是我最后的希望。

我按照现场说明的步骤

我从这里得到了我的 API 密钥

我已经做了我能做的一切。请告诉我这个过程是否不正确,我该怎么办。我正在为 Gingerbread 2.3.3 (API 10)..

等待答案谢谢。

我的日志猫:07-06 02:12:45.692:E/AndroidRuntime(3020):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.maps/com.example.maps.MainActivity}:android.view .InflateException:二进制 XML 文件第 2 行:膨胀类片段时出错

我的 Android 菜单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maps"
android:versionCode="1"
android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <!-- The following two permissions are not required to use
        Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="10" /> 

<application   
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AEdPqrEAAAAI5QAuDgiTRjyU-y_MK-ZRQqBaN_VoLb4gADuCwA"/>
</application>

我的 Main.xml

 <fragment
 xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/map"
   android:name="com.google.android.gms.maps.MapFragment"
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
    tools:context=".MainActivity" 
xmlns:tools="http://schemas.android.com/tools" />

我的 MainActivity.Java

package com.example.maps;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;

public class MainActivity extends MapActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MapView view = (MapView) findViewById(R.id.map);
    view.setBuiltInZoomControls(true);  
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}
4

2 回答 2

0

在 main.xml

不应该

android:name="com.google.android.gms.maps.MapFragment"

class="com.google.android.gms.maps.MapFragment"

于 2013-07-06T15:14:08.963 回答
0

好吧,您正在使用片段,但将其加载到 MapView 中。这就是您得到 RuntimeException 的原因,因为无法将片段膨胀到 MapView 对象中。MapView 是 android maps API 版本 1 中支持的旧类,现在已弃用。

从您引用的开发网站:

注意:Google Maps Android API v2 使用新的密钥管理系统。来自 Google Maps Android v1 应用程序(通常称为 MapView)的现有密钥不适用于 v2 API。

您的活动实际上应该扩展 MapFragment,并且您应该使用 FragmentManager 或 SupportFragmentManager 来扩充您的地图。

public class MyActivity extends FragmentActivity {
   private GoogleMap mMapView;
   private SupportMapFragment mFragment;


   @Override
   protected void onCreate() {
     super.onCreate();
     FragmentManager fragmentManager = getSupportFragmentManager();
     mFragment = ((SupportMapFragment) fragmentManager.findFragmentById(R.id.map));
     mMapView = mFragment.getMap();
     mMapView.getUiSettings().setZoomControlsEnabled(true);
     // configure other settings of map view
   }
}

此外,布局 xml 中的类名称也应该是 SupportMapFragment,如下所示:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:name="com.google.android.gms.maps.SupportMapFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  tools:context=".MainActivity" 
  xmlns:tools="http://schemas.android.com/tools" />

你可以在这里找到更多信息:https ://developers.google.com/maps/documentation/android/intro

于 2013-07-06T15:28:55.217 回答