![](/img/trans.png)
[英]Java Android Emulator: Crash when using Maps v2 (Caused by android.view.InflateException: Binary XML file line #11: Error Inflating class fragment
[英]Google Maps API V2 - Binary XML File line #16 : Error inflating class fragment
我正在嘗試使用V2 API來獲取我的拳頭Google Maps應用程序-我的IDE是Eclipse-編譯良好,在我的VDM(Nexus 7)中顯示,但是當我啟動時-它崩潰了。
清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whatson.showme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<permission
android:name="com.whatson.showme.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.whatson.showme.permission.MAPS_RECEIVE"/>
<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"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<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"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="*** MY API KEY*****"/>
<activity
android:name="com.whatson.showme.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>
</manifest>
MainActivity.java:
package com.whatson.showme;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
private final LatLng LOCATION_BURNABY = new LatLng(49.27645, -122.917587);
private final LatLng LOCATION_SURRREY = new LatLng(49.187500, -122.849000);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.addMarker(new MarkerOptions().position(LOCATION_SURRREY).title("Find me here!"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick_City(View v) {
//CameraUpdate update = CameraUpdateFactory.newLatLng(LOCATION_BURNABY);
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_BURNABY, 9);
map.animateCamera(update);
}
public void onClick_Burnaby(View v) {
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_BURNABY, 14);
map.animateCamera(update);
}
public void onClick_Surrey(View v) {
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_SURRREY, 16);
map.animateCamera(update);
}
}
activity_mail.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
class="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
日志轉儲:
12-27 01:42:32.095: E/AndroidRuntime(753): FATAL EXCEPTION: main
12-27 01:42:32.095: E/AndroidRuntime(753): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whatson.showme/com.whatson.showme.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
12-27 01:42:32.095: E/AndroidRuntime(753): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.os.Looper.loop(Looper.java:132)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.app.ActivityThread.main(ActivityThread.java:4025)
12-27 01:42:32.095: E/AndroidRuntime(753): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 01:42:32.095: E/AndroidRuntime(753): at java.lang.reflect.Method.invoke(Method.java:491)
12-27 01:42:32.095: E/AndroidRuntime(753): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-27 01:42:32.095: E/AndroidRuntime(753): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-27 01:42:32.095: E/AndroidRuntime(753): at dalvik.system.NativeStart.main(Native Method)
12-27 01:42:32.095: E/AndroidRuntime(753): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
12-27 01:42:32.095: E/AndroidRuntime(753): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
12-27 01:42:32.095: E/AndroidRuntime(753): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.app.Activity.setContentView(Activity.java:1780)
12-27 01:42:32.095: E/AndroidRuntime(753): at com.whatson.showme.MainActivity.onCreate(MainActivity.java:26)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
12-27 01:42:32.095: E/AndroidRuntime(753): ... 11 more
12-27 01:42:32.095: E/AndroidRuntime(753): Caused by: java.lang.ArrayIndexOutOfBoundsException: index=12 length=0
12-27 01:42:32.095: E/AndroidRuntime(753): at android.content.res.TypedArray.hasValue(TypedArray.java:656)
12-27 01:42:32.095: E/AndroidRuntime(753): at com.google.android.gms.maps.model.CameraPosition.createFromAttributes(Unknown Source)
12-27 01:42:32.095: E/AndroidRuntime(753): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
12-27 01:42:32.095: E/AndroidRuntime(753): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.app.Activity.onCreateView(Activity.java:4140)
12-27 01:42:32.095: E/AndroidRuntime(753): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
嘗試android:name
(而不是class
):
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
我認為SupportMapFragment
使用了class="myfragment"
,
而android:name="myfragment"
與MapFragment
一起MapFragment
。
您可以查看本教程以獲取更多詳細信息: 請參閱步驟5 。
希望這可以幫助。
您可能已經來不及了,但是對於那些尋找解決方案的人來說,該解決方案只能將該標簽作為“應用程序”的子項放在清單中
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.