我的应用已经在使用谷歌地图Android API v2,它正在我的Galaxy Mini(Android 2.3.6)上运行。当我尝试使用Platform 4.0.3,API级别15和Google API在模拟器上进行测试时,会出现以下错误:
01-23 14:26:31.581: ERROR/AndroidRuntime(688): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.remotepark/br.com.remotepark.activities.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:262)
at br.com.remotepark.activities.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
... 11 more
Caused by: java.lang.NullPointerException
at br.com.remotepark.fragments.MapFragment.initLocation(MapFragment.java:86)
at br.com.remotepark.fragments.MapFragment.onCreateView(MapFragment.java:62)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1156)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:270)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
... 22 more
抛出异常的行是:
mMap.setInfoWindowAdapter(new MapInfoWindowAdapter(getLayoutInflater(savedInstanceState)));
MapInfoWindowAdapter构造函数如下:
MapInfoWindowAdapter(LayoutInflater layoutInflater) {
mLayoutInflater = layoutInflater;
mWindow = mLayoutInflater.inflate(R.layout.map_info_window, null);
mContents = mLayoutInflater.inflate(R.layout.map_info_window, null);
}
map_info_window.xml是:
<ImageView
android:layout_width="40dip"
android:layout_height="40dip"
android:src="@drawable/ic_remote_park"
android:id="@+id/remotepark_logo"
android:layout_marginLeft="0dip"
android:layout_marginRight="8dip"
android:layout_marginTop="16dip"></ImageView>
<LinearLayout
android:id="@+id/balloon_inner_layout"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:paddingTop="5dip">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="0dip">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFF"
android:id="@+id/parking_name_info_window"
android:textSize="7pt"
android:singleLine="true"
android:textStyle="bold"
android:ellipsize="end"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/RelativeLayout2"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="2dip">
<RelativeLayout
android:id="@+id/frameLayout1"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFF"
android:id="@+id/availability_label_info_window"
android:text="@string/places_str"
android:textStyle="bold"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFF"
android:id="@+id/availability_info_window"
android:layout_toRightOf="@+id/availability_label_info_window"
android:layout_marginLeft="5dip" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/RelativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<TextView
android:id="@+id/price_label_info_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textStyle="bold"/>
<TextView
android:id="@+id/price_symbol_info_window"
android:layout_toRightOf="@+id/price_label_info_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/price_symbol_str"
android:textColor="#FFF"
android:layout_marginLeft="5dip"/>
<TextView
android:id="@+id/price_info_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/price_symbol_info_window"
android:layout_marginLeft="2dip"/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/RelativeLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="right"
android:layout_marginBottom="5dip">
</RelativeLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_more_info_disabled"
android:id="@+id/more_info_button"
android:layout_marginLeft="10dip"
android:layout_marginRight="8dip"
android:layout_marginTop="16dip"></ImageView></LinearLayout>
你知道它为什么不起作用吗?
答案 0 :(得分:0)
根据Google的说法,Google Play services API需要“物理开发设备”,因此仿真器不会正式支持它们。因此,如果您在模拟器上运行,几乎可以肯定您缺少所需的服务。人们报告让它在模拟器中工作,但我没有尝试过自己。如果您想查看在模拟器上安装Google Play,可以查看this post中的答案。
当您尝试从xml中充气MapFragment
或SupportMapFragment
时出现错误。通常,最好通过调用isGooglePlayServicesAvailable()
来检查设备是否具有Google Play服务,并确保在继续之前返回ConnectionResult.SUCCESS
。这允许您呈现错误对话框而不仅仅是崩溃。 Google甚至为其他一些代码提供了错误对话框,您可以通过getErrorDialog()
提示他们升级/安装,例如 -
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
switch (resultCode) {
case ConnectionResult.SUCCESS: // proceed
break;
case ConnectionResult.SERVICE_MISSING:
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
case ConnectionResult.SERVICE_DISABLED:
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 1);
dialog.show();
}
在致电setContentView
之前执行此操作。