1

我的应用程序已经在使用 Google Maps Android API v2,并且正在我的 Galaxy Mini (Android 2.3.6) 上运行。当我尝试在使用平台 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>

你知道为什么它不工作吗?

4

1 回答 1

0

根据 Google 的说法,Google Play 服务API 需要“物理开发设备”,因此它们不受模拟器的官方支持。因此,如果您在模拟器上运行,几乎可以肯定您缺少所需的服务。人们报告说让它在模拟器中工作,但我自己没有尝试过。如果您想在模拟器上安装 Google Play,您可以在这篇文章中查看答案。

当您尝试从您的 xml膨胀MapFragmentor时,您会收到错误消息。SupportMapFragment通过调用 检查设备是否具有 Google Play 服务通常是一个好主意isGooglePlayServicesAvailable(),并确保它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

于 2013-01-23T15:16:54.130 回答