Android Google Maps V2 giving me IllegalStateException

若如初见. 提交于 2020-01-17 08:54:45

问题


I did all of the Steps that were shown on Google and i also looked up other solution but in the i end up with this.

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

}

I really want to understand what I am doing wrong that way i can actually learn to solve these kinds of things by myself.


回答1:


In Manifest we require only the following things to show google map:

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

Change your fragment tag as:

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

Clean your project and run.




回答2:


Typo in manifest file:

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

Change verson to version.



来源:https://stackoverflow.com/questions/22248647/android-google-maps-v2-giving-me-illegalstateexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!