谷歌地图应用程序在运行时崩溃

时间:2015-03-04 12:51:31

标签: android google-maps

我尝试在Google地图上创建一个简单的项目。我已经推荐了许多教程,但不幸的是我的应用程序崩溃了,给了我以下错误。

错误:

03-04 17:15:45.766: E/AndroidRuntime(2439): FATAL EXCEPTION: main 
03-04 17:15:45.766: E/AndroidRuntime(2439): Process: com.example.myapp, PID: 2439
03-04 17:15:45.766: E/AndroidRuntime(2439): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.app.ActivityThread.access$900(ActivityThread.java:177)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.os.Looper.loop(Looper.java:145)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.app.ActivityThread.main(ActivityThread.java:5944)
03-04 17:15:45.766: E/AndroidRuntime(2439): at java.lang.reflect.Method.invoke(Native Method)
03-04 17:15:45.766: E/AndroidRuntime(2439): at java.lang.reflect.Method.invoke(Method.java:372)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
03-04 17:15:45.766: E/AndroidRuntime(2439): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
03-04 17:15:45.766: E/AndroidRuntime(2439): at     com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:435)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.app.Activity.setContentView(Activity.java:2267)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.example.myapp.MainActivity.onCreate(MainActivity.java:16)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.app.Activity.performCreate(Activity.java:6289)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
03-04 17:15:45.766: E/AndroidRuntime(2439): ... 10 more
03-04 17:15:45.766: E/AndroidRuntime(2439): Caused by:     java.lang.IllegalStateException: The meta-data tag in your app's     AndroidManifest.xml does not have the right value.  Expected 6587000 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" />
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.common.GooglePlayServicesUtil.D(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.maps.internal.x.T(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.maps.internal.x.S(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.maps.SupportMapFragment$b.nO(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.dynamic.a.a(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2158)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
03-04 17:15:45.766: E/AndroidRuntime(2439): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
03-04 17:15:45.766: E/AndroidRuntime(2439): ... 19 more

Manfest.xml

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />
<permission android:name="com.example.myapp.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<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" />
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- My Location -->
<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=".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>

     <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="my key"/>
     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>

MainActivity.java

package com.example.myapp;

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

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

3 个答案:

答案 0 :(得分:1)

崩溃说明meta-data文件中的Manifest.xml没有正确的值

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

崩溃还表示google_play_services_version的值应 6587000 ,但当前值 0

确保您正确导入Google Play Services library。这是您崩溃的问题。

答案 1 :(得分:0)

改变这个:

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

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

答案 2 :(得分:0)

清单文件中的Google地图元数据似乎存在问题。请确保您已正确添加Google Play服务 http://developer.android.com/google/play-services/setup.html