在Android上运行Google Map API时出错

时间:2014-07-26 01:52:19

标签: java android eclipse google-maps

我正在为地图使用Google Map API V2 API。我已将google-play-service libs导入为独立项目,并将其设置为我当前项目的参考。

GoogleMap Manifest

<?xml version="1.0" encoding="utf-8"?>

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

<permission android:name="com.example.googlemap.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.example.googlemap.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" />

<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.googlemap.GoogleMap"
        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="This should be correct" />

</application>

main.xml中

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GoogleMap" >

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

但是,我收到的错误是logCat:

07-26 09:46:30.191: D/AndroidRuntime(2504): Shutting down VM
07-26 09:46:30.191: W/dalvikvm(2504): threadid=1: thread exiting with uncaught exception (group=0x40c331f8)
07-26 09:46:30.199: E/AndroidRuntime(2504): FATAL EXCEPTION: main
07-26 09:46:30.199: E/AndroidRuntime(2504): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemap/com.example.googlemap.GoogleMap}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.os.Looper.loop(Looper.java:137)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.ActivityThread.main(ActivityThread.java:4512)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at java.lang.reflect.Method.invoke(Method.java:511)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at dalvik.system.NativeStart.main(Native Method)
07-26 09:46:30.199: E/AndroidRuntime(2504): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:272)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.Activity.setContentView(Activity.java:1835)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at com.example.googlemap.GoogleMap.onCreate(GoogleMap.java:12)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.Activity.performCreate(Activity.java:4465)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-26 09:46:30.199: E/AndroidRuntime(2504):     ... 11 more
07-26 09:46:30.199: E/AndroidRuntime(2504): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.Fragment.instantiate(Fragment.java:574)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.Fragment.instantiate(Fragment.java:549)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.app.Activity.onCreateView(Activity.java:4235)
07-26 09:46:30.199: E/AndroidRuntime(2504):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
07-26 09:46:30.199: E/AndroidRuntime(2504):     ... 21 more
07-26 09:46:38.152: I/Process(2504): Sending signal. PID: 2504 SIG: 9

GoogleMap.java

package com.example.googlemap;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

公共类GoogleMap扩展了Activity {

// 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 = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

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

它还告诉我MapFragment cannot be resolved to a type错误消息。

任何指南?提前谢谢。

1 个答案:

答案 0 :(得分:0)

在调用地图SupportMapFragment时使用Fragment,这是您在布局上声明的类的名称,如下所示:

googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();

不要忘记导入SupportMapFragment类。

修改:也使用SupportFragmentManager,这是FragmentManager的支持版本。

Edit2:您必须使您的活动扩展FragmentActivity,以便您可以使用getSupportFragmentManager。