android: Binary XML file line #9: error inflating class fragment

匿名 (未验证) 提交于 2019-12-03 02:25:01

问题:

I was trying to show the maps in android application. I am using map V2. But the error "Binary XML file line #9: error inflating class fragment" is thrown. The app is crashed on launch.

Below is my code. Please help me in solving this stuff

MainActivity.Java

    package com.example.googlemapsv2;     import android.os.Bundle;     import android.support.v4.app.FragmentActivity;      public class MainActivity extends FragmentActivity {        @Override       protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);     }    } 

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:map="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >  <fragment     android:name="com.google.android.gms.maps.SupportMapFragment"     android:id="@+id/map"     android:layout_width="match_parent"     android:layout_height="match_parent" /> </LinearLayout> 

Android manifest

 <?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.googlemapsv2" android:versionCode="1" android:versionName="1.0" >  <permission     android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE"     android:protectionLevel="signature" />  <uses-permission android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE" /> <uses-sdk     android:minSdkVersion="12"     android:targetSdkVersion="18" /> <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" /> <!-- Required OpenGL ES 2.0. for Maps V2 --> <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="com.example.googlemapsv2.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="AIzaSyBAvQK4RAin10dqyflr95tH45Ce_Eko3G0" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </manifest> 

回答1:

The below must be inside application tag of manifest file

Reference

https://developers.google.com/maps/documentation/android/start#add_the_google_play_services_version_to_your_apps_manifest

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

Since your minsdk is 12. You need to use MapFragment and your class should extend Activity.

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

Then

public class MainActivity extends Activity 

Make sure you reference the library project properly and enabled maps for android in the google api console.



回答2:

Try this with SupportMapFragment activity_main.xml

change android:name to simple class

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

MainActivity.java

public class MainActivity extends FragmentActivity {      // Google Map     private GoogleMap googleMap;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          try {             // Loading map             initializeMap();          } catch (Exception e) {             e.printStackTrace();         }      }      /**      * function to load map. If map is not created it will create it for you      * */     private void initializeMap() {         if (googleMap == null) {             googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();             // check if map is created successfully or not             if (googleMap == null) {                 Toast.makeText(getApplicationContext(),                     "Sorry! unable to create maps", Toast.LENGTH_SHORT)                     .show();             }         }     }      @Override     protected void onResume() {         super.onResume();     } } 

Change SDK version

<uses-sdk     android:minSdkVersion="8"     android:targetSdkVersion="17" /> 

Raghunandan is right , you should write <meta>tag code inside the <application>.



回答3:

In my experience formerly API 18/19 must adding in menifest -> application meta-data Q update updated with the version:

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



回答4:

I had the same problem,I solved it by adding permissions as specified in the official doc (Google map doc on permissions)

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



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