Android地图加载错误

时间:2013-12-19 05:14:58

标签: android android-layout android-fragments android-maps-v2

我是android地图开发的新手。我试图将谷歌地图添加到我的应用程序。它产生了错误。发生了以下错误。

12-19 10:53:47.950: E/AndroidRuntime(4131): FATAL EXCEPTION: main
12-19 10:53:47.950: E/AndroidRuntime(4131): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maptest/com.example.maptest.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.os.Looper.loop(Looper.java:137)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.ActivityThread.main(ActivityThread.java:4960)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at java.lang.reflect.Method.invokeNative(Native Method)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at java.lang.reflect.Method.invoke(Method.java:511)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at dalvik.system.NativeStart.main(Native Method)
12-19 10:53:47.950: E/AndroidRuntime(4131): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:318)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.Activity.setContentView(Activity.java:1925)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.example.maptest.MainActivity.onCreate(MainActivity.java:12)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.Activity.performCreate(Activity.java:5203)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
12-19 10:53:47.950: E/AndroidRuntime(4131):     ... 11 more
12-19 10:53:47.950: E/AndroidRuntime(4131): Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  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" />
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.maps.internal.q.v(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.maps.internal.q.u(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.maps.MapFragment$b.cE(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.dynamic.a.a(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.app.Activity.onCreateView(Activity.java:4835)
12-19 10:53:47.950: E/AndroidRuntime(4131):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
12-19 10:53:47.950: E/AndroidRuntime(4131):     ... 21 more

这是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" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="145dp" />

</RelativeLayout>

这是主要的活动代码。

public class MainActivity extends Activity {

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

    @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, menu);
        return true;
    }

}

这是清单代码。

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />



    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

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



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

        <uses-library
        android:name="com.google.android.maps"
        android:required="false" />

        <activity
            android:name="com.example.maptest.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>
    </application>

</manifest>

我也添加了google_play_service库。

任何人都可以帮我解决这个错误。

4 个答案:

答案 0 :(得分:1)

Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  
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" />

你的日志告诉你一切:

<application>内添加Google Play服务的版本代码,即在</application>AndroidManifest.xml之前

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

Reference

同时在<application>内移动以下代码,即</application>内的AndroidManifest.xml

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

答案 1 :(得分:1)

如果您知道要在地图中显示的位置名称,则可以尝试使用此代码。它对我有用。

Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(this, Locale.getDefault());
try {
    //getFromLocationName returns array of address, it takes 2 parameter one place name and count of result
    addresses = geocoder.getFromLocationName("Place name", 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

latitude = addresses.get(0).getLatitude();
longitude = addresses.get(0).getLongitude();
String label = "SomePlace";
String in = "geo:" + latitude + "," + longitude;
String query = latitude + "," + longitude + "(" + label + ")";
String ey = Uri.encode(query);
String uriLoc = in + "?q=" + ey+ "&z=16";
Log.d(uriLoc, "no log");
Uri uri = Uri.parse(uriLoc);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
startActivity(intent);

答案 2 :(得分:0)

请从日志中查看此行。

引起:java.lang.IllegalStateException:应用程序的AndroidManifest.xml中不需要的元数据标记不存在。您必须在应用程序标记

中包含以下声明
 <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

答案 3 :(得分:0)

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

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <permission
        android:name="com.example.map.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <!--to access information about networks-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

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

    <!--to access approximate location through network location providers-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <!--to get precise location through GPS location provider-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <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" />


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

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MainActivity"
            android:label="@string/title_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>
</manifest>