如何在Android中集成Google Map?

时间:2015-01-12 08:59:04

标签: android google-maps

我想在Android应用中实施Google Map。我还按照Google控制台api的所有步骤操作。 但在运行时,我的应用程序崩溃并得到有关片段的错误,我不明白。我还在app中添加了google play服务库。有人请帮助我。谢谢。提前感谢。

这是我的日志信息。

01-12 14:05:57.606: W/dalvikvm(451): Unable to resolve superclass of Lcom/google/android/gms/maps/MapFragment; (30)
01-12 14:05:57.606: W/dalvikvm(451): Link of class 'Lcom/google/android/gms/maps/MapFragment;' failed
01-12 14:05:57.616: D/AndroidRuntime(451): Shutting down VM
01-12 14:05:57.616: W/dalvikvm(451): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-12 14:05:57.646: E/AndroidRuntime(451): FATAL EXCEPTION: main
01-12 14:05:57.646: E/AndroidRuntime(451): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example_map.demomap/com.example_map.demomap.Activity_Map}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.os.Looper.loop(Looper.java:123)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-12 14:05:57.646: E/AndroidRuntime(451):  at java.lang.reflect.Method.invokeNative(Native Method)
01-12 14:05:57.646: E/AndroidRuntime(451):  at java.lang.reflect.Method.invoke(Method.java:507)
01-12 14:05:57.646: E/AndroidRuntime(451):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-12 14:05:57.646: E/AndroidRuntime(451):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-12 14:05:57.646: E/AndroidRuntime(451):  at dalvik.system.NativeStart.main(Native Method)
01-12 14:05:57.646: E/AndroidRuntime(451): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-12 14:05:57.646: E/AndroidRuntime(451):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.app.Activity.setContentView(Activity.java:1657)
01-12 14:05:57.646: E/AndroidRuntime(451):  at com.example_map.demomap.Activity_Map.onCreate(Activity_Map.java:18)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-12 14:05:57.646: E/AndroidRuntime(451):  ... 11 more
01-12 14:05:57.646: E/AndroidRuntime(451): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example_map.demomap-1.apk]
01-12 14:05:57.646: E/AndroidRuntime(451):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
01-12 14:05:57.646: E/AndroidRuntime(451):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
01-12 14:05:57.646: E/AndroidRuntime(451):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.view.LayoutInflater.createView(LayoutInflater.java:471)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
01-12 14:05:57.646: E/AndroidRuntime(451):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-12 14:05:57.646: E/AndroidRuntime(451):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
01-12 14:05:57.646: E/AndroidRuntime(451):  ... 20 more

这是活动代码

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

public class Activity_Map extends FragmentActivity 
{

    // Google Map
    private GoogleMap googleMap;
   //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
}

Manifest.xml文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example_map.demomap"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

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

        <activity
            android:name="com.example_map.demomap.Activity_Map"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDRr331QyQy0lyqCivgpv6NdfUzL7Z2Wwg" />
    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

你应该

  <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

<application>

中的manifest.xml标记中

答案 1 :(得分:1)

您应该在Manifest.xml文件中添加此权限

<permission
    android:name="com.example_map.demomap.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example_map.demomap.permission.MAPS_RECEIVE" />

并在<meta-data>

中添加此<application>
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

有关详细信息,请参阅this tutorial.