我正在尝试在我的模拟器和设备上加载 map View
我已经尝试了两天,但有时我只是在没有任何 map 的情况下得到网格线,有时会出现 fatal error ,并且我的应用程序无法在我的模拟器和设备上启动。
我尝试了互联网上存在的所有代码,但没有任何效果。 你是我最后的希望。
我按照 site 上所述的步骤进行操作
我从 here 获取了我的 API key
我已经做了我能做的一切。请告诉我,如果这个过程不正确,我该怎么办。我正在为 Gingerbread 2.3.3 (API 10) 做这件事..
等待答案,谢谢。
我的原木猫: 07-06 02:12:45.692:E/AndroidRuntime(3020):java.lang.RuntimeException:无法启动 Activity ComponentInfo {com.example.maps/com.example.maps.MainActivity}:android.view.InflateException:二进制XML 文件第 2 行:膨胀类 fragment 时出错
我的 AndroidMenifest:
<?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;
}
}
最佳答案
好吧,您正在使用一个 fragment ,但将其加载到 MapView 中。这就是您收到 RuntimeException 的原因,因为不可能将 fragment 膨胀到 MapView 对象中。 MapView 是一个旧类,在 Android map API 版本 1 中受支持,现已弃用。
来自您引用的开发网站:
Note: The Google Maps Android API v2 uses a new system of managing keys. Existing keys from a Google Maps Android v1 application, commonly known as MapView, will not work with the v2 API.
您的 Activity 实际上应该扩展 MapFragment,并且您应该使用 FragmentManager 或 SupportFragmentManager 来扩充您的 map 。
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
关于java - 无法在模拟器和设备上加载 Android Mapview Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17503096/