我正在尝试制作简单的谷歌地图应用程序,但每当我启动时它都会崩溃..
mainactivity.java
package com.example.google;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.support.v7.app.ActionBarActivity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
GoogleMap map=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(map==null)
{
map=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
}
activitymain.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="com.example.google.MainActivity" >
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</RelativeLayout>
manifext.example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.google"
android:versionCode="1"
android:versionName="1.0" >
<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" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<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>
</application>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyClHjmO6KkTTnr0r4vu7zvDNc-6KVDWwEA" />
</manifest>
的logcat ..
07-23 12:05:27.513: E/AndroidRuntime(23615): FATAL EXCEPTION: main
07-23 12:05:27.513: E/AndroidRuntime(23615): Process: com.example.google, PID: 23615
07-23 12:05:27.513: E/AndroidRuntime(23615): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.google/com.example.google.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2712)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2777)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.ActivityThread.access$900(ActivityThread.java:179)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.os.Handler.dispatchMessage(Handler.java:102)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.os.Looper.loop(Looper.java:145)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.ActivityThread.main(ActivityThread.java:5972)
07-23 12:05:27.513: E/AndroidRuntime(23615): at java.lang.reflect.Method.invoke(Native Method)
07-23 12:05:27.513: E/AndroidRuntime(23615): at java.lang.reflect.Method.invoke(Method.java:372)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
07-23 12:05:27.513: E/AndroidRuntime(23615): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.example.google.MainActivity.onCreate(MainActivity.java:17)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.Activity.performCreate(Activity.java:6289)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
07-23 12:05:27.513: E/AndroidRuntime(23615): ... 10 more
07-23 12:05:27.513: E/AndroidRuntime(23615): 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" />
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.common.GooglePlayServicesUtil.zzO(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.maps.internal.zzy.zzai(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.maps.internal.zzy.zzah(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.maps.MapFragment$zzb.zztD(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.maps.MapFragment$zzb.zza(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.dynamic.zza.zza(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2130)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.app.Activity.onCreateView(Activity.java:5610)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:79)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:804)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
07-23 12:05:27.513: E/AndroidRuntime(23615): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)
07-23 12:05:27.513: E/AndroidRuntime(23615): ... 20 more
答案 0 :(得分:0)
的哪一部分
你不明白吗?您只声明了API密钥,您也应该添加Play服务版本。您应用的AndroidManifest.xml中的必需元数据标记不存在。您必须在&lt; application&gt;中包含以下声明: element:&lt; meta-data android:name =“com.google.android.gms.version”android:value =“@ integer / google_play_services_version”/&gt;