错误而在谷歌地图片段充气XML [英] error while inflating XML in Google maps fragment
问题描述
尝试使用一个片段显示谷歌地图。用下面的页面作为一个教程。
我收到异常错误充气类片段。
1)进口的jar谷歌播放services.jar
2)下载并配置了谷歌播放服务SDK。
3)获得最新的V2 API密钥。
4)加在清单中的权限com.google.android.providers.gsf.permission.READ_GSERVICES。
5)使用mindsdk = 8,目标= 16。
有关参考,的AndroidManifest.xml
:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.com.mapsdemo
安卓版code =1
机器人:VERSIONNAME =1.0>
<使用特征安卓glEsVersion =0x00020000机器人:要求=真/>
<许可
机器人:名称=com.example.com.mapsdemo.permission.MAPS_RECEIVE
安卓的ProtectionLevel =签名/>
<使用-权限的Android:名称=com.example.com.mapsdemo.permission.MAPS_RECEIVE/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<使用-权限的Android:名称=com.google.android.providers.gsf.permission.READ_GSERVICES/>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =16/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.com.mapsdemo.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
&所述;元数据
机器人:名称=com.google.android.maps.v2.API_KEY
机器人:值=AIzaSyDXPsxWF634gd907NzZKkRkNS0oH9IPWgk/>
< /用途>
MainActivity.java
包com.example.com.mapsdemo;
进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.widget.Toast;
进口com.google.android.gms.common.GooglePlayServicesUtil;
// API:AIzaSyDXPsxWF634gd907NzZKkRkNS0oH9IPWgk
公共类MainActivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
尝试 {
的setContentView(R.layout.activity_main);
}赶上(例外五){
Toast.makeText(这一点,e.toString(),Toast.LENGTH_LONG).show();
}
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}
}
activity_main.xml
< XML版本=1.0编码=UTF-8&GT?;
<片段的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /图
机器人:layout_width =match_parent
机器人:layout_height =match_parent
类=com.google.android.gms.maps.MapFragment/>
错误堆栈跟踪:
3月一日至五日:51:46.260:E / YOUR_APP_LOG_TAG(19189):在com.example.com.mapsdemo.MainActivity.onCreate(MainActivity.java:22)
3月一号至五号:52:31.135:E / AndroidRuntime(19446):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.com.mapsdemo / com.example.com.mapsdemo.MainActivity}:android.view .InflateException:二进制XML文件中的行#2:错误充气类片段
3月一号至五号:52:31.135:E / AndroidRuntime(19446):在com.example.com.mapsdemo.MainActivity.onCreate(MainActivity.java:22)
3月一号至五号:57:43.745:E / AndroidRuntime(19781):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.com.mapsdemo / com.example.com.mapsdemo.MainActivity}:android.view .InflateException:二进制XML文件中的行#2:错误充气类片段
3月一号至五号:57:43.745:E / AndroidRuntime(19781):在com.example.com.mapsdemo.MainActivity.onCreate(MainActivity.java:22)
3月一号至五号:58:15.737:E / YOUR_APP_LOG_TAG(19886):在com.example.com.mapsdemo.MainActivity.onCreate(MainActivity.java:22)
logcat的:
01-02 18:31:21.477:I / JDWP(11473):忽略第二调试器 - 接受和丢弃
01-02 18:31:24.493:D / libEGL(11871):加载/vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
01-02 18:31:24.493:D / libEGL(11871):加载/vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-02 18:31:24.509:D / libEGL(11871):加载/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-02 18:31:24.602:D / OpenGLRenderer(11871):启用调试模式0
错误充气类片段
通常是当你试图使用本机API级别11版片段 - 这源$ C $ C在做什么 - 但试图在Android上的旧版本中运行它。为了使用<片断>
的API等级10及以下,你必须使用片段支持Android包的反向移植:
- 添加支持Android包到你的项目
- 从
FragmentActivity
继承,而不是活动
- 使用
SupportMapFragment
,而不是MapFragment
- 在改变一切,现在有所不同基于这种反向移植
或者,你可以设置你的安卓的minSdkVersion
11或更高,运行目前的code,仅仅只在较新设备
您可以了解更多关于文档一>
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"/>
Error Stack Trace :
01-05 03:51:46.260: E/YOUR_APP_LOG_TAG(19189): at com.example.com.mapsdemo.MainActivity.onCreate(MainActivity.java:22)
01-05 03:52:31.135: E/AndroidRuntime(19446): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.mapsdemo/com.example.com.mapsdemo.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
01-05 03:52:31.135: E/AndroidRuntime(19446): at com.example.com.mapsdemo.MainActivity.onCreate(MainActivity.java:22)
01-05 03:57:43.745: E/AndroidRuntime(19781): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.mapsdemo/com.example.com.mapsdemo.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
01-05 03:57:43.745: E/AndroidRuntime(19781): at com.example.com.mapsdemo.MainActivity.onCreate(MainActivity.java:22)
01-05 03:58:15.737: E/YOUR_APP_LOG_TAG(19886): at com.example.com.mapsdemo.MainActivity.onCreate(MainActivity.java:22)
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
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.
这篇关于错误而在谷歌地图片段充气XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!