问题
trying to display Google map using a fragment. Used the following page as a tutorial.
I am getting the exception "Error Inflating class fragment ".
1) Imported the jar google-play-services.jar
2) downloaded and configured the google play services SDK.
3) got the latest v2 API Key.
4) Added permission com.google.android.providers.gsf.permission.READ_GSERVICES in the manifest.
5) using mindsdk = 8 and target = 16.
For a reference, Androidmanifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.mapsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<permission
android:name="com.example.com.mapsdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.com.mapsdemo.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-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.com.mapsdemo.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="AIzaSyDXPsxWF634gd907NzZKkRkNS0oH9IPWgk"/>
</application>
MainActivity.java
package com.example.com.mapsdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesUtil;
//API : AIzaSyDXPsxWF634gd907NzZKkRkNS0oH9IPWgk
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_main);
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.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.MapFragment"/>
Logcat :
01-02 18:31:21.477: I/jdwp(11473): Ignoring second debugger -- accepting and dropping
01-02 18:31:24.493: D/libEGL(11871): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
01-02 18:31:24.493: D/libEGL(11871): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-02 18:31:24.509: D/libEGL(11871): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-02 18:31:24.602: D/OpenGLRenderer(11871): Enabling debug mode 0
回答1:
Error inflating class fragment
usually comes when you are trying to use the native API Level 11 version of fragments -- which your source code is doing -- but are trying to run it on an older version of Android. In order to use <fragment>
on API Level 10 and below, you have to use the Android Support package's backport of fragments:
- add the Android Support package to your project
- inherit from
FragmentActivity
instead ofActivity
- use
SupportMapFragment
rather thanMapFragment
- change anything else that now differs based upon this backport
Or, you can set your android:minSdkVersion
to 11 or higher, and run your current code, just only on newer devices.
You can read more about the Android Support package in the documentation.
回答2:
I was getting same error. Check that you run it on real device.and atatch google play services library in following way. project_properties->android-> Library then add google play lib there..
again make libs folder copy android support v4.jar and google_play_services.jar and then right click to each file and select build to path..
also check your api key is for com.example.com.mapsdemo package..for every package new api key is to be generated..
and use SupportMapFragment instead on MapFragment in your XML file.
this solved my problem.
Hope this solve your problem.
回答3:
A lot of questions about this problem, and after twi days, my solution is simple: you have to compile your project using Project Build Target Google API. I feel so stupid about it.
来源:https://stackoverflow.com/questions/14122705/error-while-inflating-xml-in-google-maps-fragment