我知道这个问题已经被问了数百次了,感觉就像我已经通过它们都没有找到答案。
我一直在关注如何向 googlemaps 添加标记的答案。如何在谷歌地图 API v2 中添加叠加层 | 安卓 |
我没有收到任何错误,但是当我运行应用程序时,我得到“程序已停止”,并且我在 logcat 中收到这些错误:
01-08 13:22:20.658: W/dalvikvm(9338): threadid=1: thread exiting with uncaught exception (group=0x40c701f8)
01-08 13:22:20.663: E/AndroidRuntime(9338): FATAL EXCEPTION: main
01-08 13:22:20.663: E/AndroidRuntime(9338): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.googlemaps/com.googlemaps.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.os.Looper.loop(Looper.java:137)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-08 13:22:20.663: E/AndroidRuntime(9338): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 13:22:20.663: E/AndroidRuntime(9338): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 13:22:20.663: E/AndroidRuntime(9338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-08 13:22:20.663: E/AndroidRuntime(9338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-08 13:22:20.663: E/AndroidRuntime(9338): at dalvik.system.NativeStart.main(Native Method)
01-08 13:22:20.663: E/AndroidRuntime(9338): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-08 13:22:20.663: E/AndroidRuntime(9338): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:272)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.app.Activity.setContentView(Activity.java:1835)
01-08 13:22:20.663: E/AndroidRuntime(9338): at com.googlemaps.MainActivity.onCreate(MainActivity.java:19)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.app.Activity.performCreate(Activity.java:4470)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-08 13:22:20.663: E/AndroidRuntime(9338): ... 11 more
01-08 13:22:20.663: E/AndroidRuntime(9338): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.supportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
01-08 13:22:20.663: E/AndroidRuntime(9338): ... 21 more
01-08 13:22:20.663: E/AndroidRuntime(9338): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.supportMapFragment
01-08 13:22:20.663: E/AndroidRuntime(9338): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-08 13:22:20.663: E/AndroidRuntime(9338): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-08 13:22:20.663: E/AndroidRuntime(9338): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-08 13:22:20.663: E/AndroidRuntime(9338): at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
01-08 13:22:20.663: E/AndroidRuntime(9338): ... 24 more
这是我的主要java:
package com.googlemaps;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MapActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LatLng sala = new LatLng(59.918434,16.618195);
GoogleMap mMap;
mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(android.R.id.content))).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.addMarker(new MarkerOptions()
.position(sala)
.title("Här ligger sala")
.snippet("Detta är sala")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sala,10));
}
这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.supportMapFragment"
/>
</RelativeLayout>
还有我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.googlemaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<permission
android:name="com.googlemaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.googlemaps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:required="true"
android:name="com.google.android.maps" />
<activity
android:name="com.go8oglemaps.MapActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- adds google API key to manifest -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDSVPzsvOratkVZoMEKa0sF_GLOtcFI7yg"
/>
</application>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
</manifest>