Android Google Maps V2给了我IllegalStateException

时间:2014-03-07 11:17:32

标签: android google-maps google-play-services android-maps-v2

我做了所有在Google上显示的步骤,我也查找了其他解决方案,但最终还是以此为基础。

  03-07 05:04:48.318: E/AndroidRuntime(21463): FATAL EXCEPTION: main
03-07 05:04:48.318: E/AndroidRuntime(21463): Process: com.graft, PID: 21463
03-07 05:04:48.318: E/AndroidRuntime(21463): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.graft/com.graft.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.os.Looper.loop(Looper.java:136)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at java.lang.reflect.Method.invokeNative(Native Method)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at java.lang.reflect.Method.invoke(Method.java:515)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at dalvik.system.NativeStart.main(Native Method)
03-07 05:04:48.318: E/AndroidRuntime(21463): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.Activity.setContentView(Activity.java:1929)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.graft.MainActivity.onCreate(MainActivity.java:27)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.Activity.performCreate(Activity.java:5231)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-07 05:04:48.318: E/AndroidRuntime(21463):    ... 11 more
03-07 05:04:48.318: E/AndroidRuntime(21463): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4242000 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-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.maps.internal.q.v(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.maps.internal.q.u(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.maps.MapFragment$b.ex(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.dynamic.a.a(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.app.Activity.onCreateView(Activity.java:4785)
03-07 05:04:48.318: E/AndroidRuntime(21463):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
03-07 05:04:48.318: E/AndroidRuntime(21463):    ... 21 more

的Manifest.xml

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


    <permission
         android:name="com.graft.permission.MAPS_RECEIVE"
         android:protectionLevel="signature"></permission>
    <permission android:name = "com.graft.permission.MEDIA_CONTENT_CONTROL"
        android:protectionLevel="signature"></permission>
<uses-permission  android:name= "com.graft.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<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"/>
        <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <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" />
              <meta-data
   android:name="com.google.android.maps.v2.API_KEY"
   android:value="keyhere"/>
        <activity
            android:name="com.graft.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.gms.verson"
        android:value ="@integer/google_play_services_version"/>
</application>

main.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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"

        class="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>

MainActivity.java

public class MainActivity extends FragmentActivity {

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

}

我真的很想知道我做错了什么,我才能真正学会自己解决这些问题。

2 个答案:

答案 0 :(得分:1)

在Manifest中,我们只需要以下内容即可显示谷歌地图:

<manifest ….>
      <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="com.google.android.providers.gsf.permission.READ_GSERVICES" />

      <application ….>
          <activity … />
          <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="your api key" />
      </application>

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

</manifest>

将您的片段代码更改为:

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

清理项目并运行。

答案 1 :(得分:0)

清单文件中的错字:

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

verson更改为version