Google Maps API v2的片段错误

时间:2015-03-16 19:24:23

标签: java android google-maps android-fragments google-maps-android-api-2

我正在尝试制作一个涉及Google Maps API的更复杂的应用,但我开始时很简单,并试图在我的第二个活动上显示地图。 我已经看过一些教程并做了他们说的但是我一直看到这个例外:

03-16 16:17:25.645: E/AndroidRuntime(6665): FATAL EXCEPTION: main
03-16 16:17:25.645: E/AndroidRuntime(6665): Process: com.geoloctarna, PID: 6665
03-16 16:17:25.645: E/AndroidRuntime(6665): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geoloctarna/com.geoloctarna.Mapa}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.ActivityThread.access$900(ActivityThread.java:161)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.os.Looper.loop(Looper.java:157)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.ActivityThread.main(ActivityThread.java:5356)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at java.lang.reflect.Method.invoke(Method.java:515)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at dalvik.system.NativeStart.main(Native Method)
03-16 16:17:25.645: E/AndroidRuntime(6665): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.Activity.setContentView(Activity.java:2010)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at com.geoloctarna.Mapa.onCreate(Mapa.java:9)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.Activity.performCreate(Activity.java:5426)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
03-16 16:17:25.645: E/AndroidRuntime(6665):     ... 11 more
03-16 16:17:25.645: E/AndroidRuntime(6665): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.google.android.gms.maps.SupportMapFragment that is not a Fragment
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.Fragment.instantiate(Fragment.java:585)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.Fragment.instantiate(Fragment.java:561)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.app.Activity.onCreateView(Activity.java:4964)
03-16 16:17:25.645: E/AndroidRuntime(6665):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
03-16 16:17:25.645: E/AndroidRuntime(6665):     ... 21 more
03-16 16:17:25.645: E/AndroidRuntime(6665): Caused by: java.lang.ClassCastException
03-16 16:17:25.645: E/AndroidRuntime(6665):     ... 25 more

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.geoloctarna"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
                android:name="org.example.androidmaps.permission.MAPS_RECEIVE"
                android:protectionLevel="signature"/>

            <uses-permission
                android:name="org.example.ejemplogooglemaps.permission.MAPS_RECEIVE"/>

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

            <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="android.permission.ACCESS_COARSE_LOCATION"/>
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


            <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



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

    <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=".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>
        <activity android:name="com.geoloctarna.Mapa"/>

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

         <meta-data
         android:name="com.google.android.maps.v2.API_KEY"
         android:value="AIzaSyCuDKWMbbwGbwJ1yhpOQFtrBPEv4iYjlDE"/>

    </application>

</manifest>

活动代码:

package com.geoloctarna;

import android.app.Activity;
import android.os.Bundle;
public class Mapa extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapa);
    }
}

和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"
        android:name="com.geoloctarna.MainActivity"
        class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

错误一直在谈论“XML二进制文件”,但我不知道是哪个文件。我已经看到很多其他问题,但没有回答对我有帮助,所以谢谢你们。

1 个答案:

答案 0 :(得分:0)

来自:

https://developers.google.com/maps/documentation/android/start

布局应该是:

 <?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"
      android:name="com.google.android.gms.maps.MapFragment"/>