在Google Maps V2上输入类片段时出错

时间:2014-02-18 12:11:05

标签: android google-maps android-layout fragment

我开发了一个应用程序,我需要查看Google Maps v2 主要课程是:

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.app.Activity; 

public class MapsV2 extends FragmentActivity {     

private GoogleMap mMap;

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

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);
    Marker ciu = mMap.addMarker(new MarkerOptions()
                              .position(CIU).title("My Office"));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_page_option_menu, menu);
    return true;
    }
}

XML是:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"
      android:name="com.app.reminder.MapsV2"/>

所以,当我在我的设备上运行时,我收到了错误消息:

  02-18 14:20:37.596: E/AndroidRuntime(30383): FATAL EXCEPTION: main
02-18 14:20:37.596: E/AndroidRuntime(30383): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.reminder/com.app.reminder.MapsV2}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.os.Looper.loop(Looper.java:137)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at java.lang.reflect.Method.invokeNative(Native Method)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at java.lang.reflect.Method.invoke(Method.java:511)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at dalvik.system.NativeStart.main(Native Method)
02-18 14:20:37.596: E/AndroidRuntime(30383): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.app.Activity.setContentView(Activity.java:1924)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.app.reminder.MapsV2.onCreate(MapsV2.java:22)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.app.Activity.performCreate(Activity.java:5206)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
02-18 14:20:37.596: E/AndroidRuntime(30383):    ... 11 more
02-18 14:20:37.596: E/AndroidRuntime(30383): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4132500 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.maps.internal.q.v(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.maps.internal.q.u(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.maps.SupportMapFragment$b.eb(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.dynamic.a.a(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
02-18 14:20:37.596: E/AndroidRuntime(30383):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-18 14:20:37.596: E/AndroidRuntime(30383):    ... 20 more

清单是:

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

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />
<permission
    android:name="com.app.reminder.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.app.reminder.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="com.app.reminder.InfoManager">
     >
    <activity
        android:name="SplashScreen"
        android:theme="@android:style/Theme.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

 <activity android:name="MapsV2"></activity><uses-library android:name="com.google.android.maps"    
 />
 <uses-library android:name="com.google.android.maps" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBDtXP5sYucD8z5pjaJFuvt43sLWOGzyuQ" />
    <activity android:name="MapsV2"></activity>
</application>
</manifest>

我尝试了StackOverFlow中答案中的所有选项。

3 个答案:

答案 0 :(得分:1)

你应该替换这个

 mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

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

答案 1 :(得分:1)

日志很清楚,你在清单中缺少这一行:

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

正如您可以在本行末尾阅读:

Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4132500 but found 0.  You must have the following declaration within the <application> element:<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

另外,请注意使用最新版本的Google Play服务(版本13或更高版本),而不是不包含该参数的Froyo 的Google Play服务。

答案 2 :(得分:0)

替换你的片段

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

在manifest.xml中

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

活动类

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

GoogleMap gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();