尝试使用 fragment 显示Google map 。使用以下page作为教程。
我收到异常“错误膨胀类 fragment ”。
1)导入jar google-play-services.jar
2)下载并配置了Google Play服务SDK。
3)获得了最新的v2 API key 。
4)在 list 中添加了com.google.android.providers.gsf.permission.READ_GSERVICES权限。
5)使用mindsdk = 8,目标= 16。
作为引用,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
最佳答案
Error inflating class fragment
通常在您尝试使用 native API Level 11版本的 fragment 时执行-您的源代码正在执行-但尝试在较旧版本的Android上运行它。为了在API级别10及更低版本上使用<fragment>
,您必须使用Android支持程序包的 fragment 反向移植:
FragmentActivity
而不是Activity
SupportMapFragment
而不是MapFragment
或者,您可以将
android:minSdkVersion
设置为11或更高版本,并仅在较新的设备上运行当前代码。您可以在the documentation中了解有关Android支持程序包的更多信息。
关于android - Google Maps fragment 中的XML膨胀时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14122705/