【问题标题】:Google Map V2 android error Inflating class fragment errorGoogle Map V2 android错误Inflating class fragment错误
【发布时间】:2013-04-09 08:52:05
【问题描述】:

我正在使用选项卡创建一个应用程序,并且我在其中一个选项卡中有地图,当我从中打开地图时工作正常,当我访问其他选项卡时它也工作正常,但是当我回到地图时选项卡应用程序因此错误而崩溃。

04-09 14:10:43.866: E/AndroidRuntime(28184): android.view.InflateException: Binary XML file line #42: Error inflating class fragment
04-09 14:10:43.866: E/AndroidRuntime(28184):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
04-09 14:10:43.866: E/AndroidRuntime(28184):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-09 14:10:43.866: E/AndroidRuntime(28184):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-09 14:10:43.866: E/AndroidRuntime(28184):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-09 14:10:43.866: E/AndroidRuntime(28184):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-09 14:10:43.866: E/AndroidRuntime(28184):    at com.research.fragmenttabstudy.tabA.TodaysDealLocation.onCreateView(TodaysDealLocation.java:43)

这是我的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.d("err","todaysdeal location");
        Log.d("err","todaysdeal location"+inflater.toString()+" "+container.toString()+" ");
        super.onCreateView(inflater, container, savedInstanceState); 
//      map.clear();
        View view = inflater.inflate(R.layout.todays_deal_location, container,
                false);

        Log.d("err",view.toString());
        back = (ImageView) view.findViewById(R.id.list);
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                // Constants.passcode_chk = 0;
                // finish();
                mActivity.popFragments();
            }
        });
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        map = ((SupportMapFragment) getFragmentManager().findFragmentById(
                R.id.mapp)).getMap();
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    }

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/topbar" />

    <!-- <ImageView -->
    <!-- android:id="@+id/deallistmenu" -->
    <!-- android:layout_width="wrap_content" -->
    <!-- android:layout_height="wrap_content" -->
    <!-- android:layout_marginLeft="5dp" -->
    <!-- android:layout_marginTop="9dp" -->
    <!-- android:src="@drawable/deallist_menubtn" /> -->

    <ImageView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="7dp"
        android:layout_marginTop="7dp"
        android:src="@drawable/map_list" />

    <RelativeLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/imageView1"
        android:layout_marginTop="-4.8dp" >

        <!-- box layout.................................................................... -->


        <!-- box layout.................................................................... -->

        <fragment
            android:id="@+id/mapp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/tv_location"
            android:name="com.google.android.gms.maps.SupportMapFragment" />

        <!-- <ZoomControls -->
        <!-- android:id="@+id/zoomControls" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:layout_alignParentBottom="true" -->
        <!-- android:layout_alignParentLeft="true" -->
        <!-- android:layout_marginBottom="30dp" -->
        <!-- android:layout_marginLeft="100dp" /> -->

    </RelativeLayout>

    <TextView
        android:id="@+id/barTitle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:lines="1"
        android:maxLines="1"
        android:paddingLeft="50dip"
        android:paddingRight="55dip"
        android:singleLine="true"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white"
        android:textSize="18dp" />

</RelativeLayout>

【问题讨论】:

  • 检查 xml 中的第 42 行。或在此处发布您的 xml..

标签: android google-maps android-fragments android-fragmentactivity layout-inflater


【解决方案1】:

您的问题是您已经在 XML 中声明了 google maps v2 片段,因此每次您扩充 XML 时都会生成一个新片段。但是,片段的 id 在 XML 中是硬编码的,因此它会尝试使用与已经运行的片段相同的 id 创建新片段。两个正在运行的片段不能具有相同的 id,因此应用程序崩溃。

解决此问题的最佳方法是以编程方式制作地图片段。可以在 Google 地图示例项目的 ProgrammaticDemoActivity.java 部分中找到一个很好的示例。

但是,如果由于某种原因您必须在 XML 中声明您的片段,您可以通过在离开视图时杀死旧的地图片段来使其工作,以便可以创建新的片段。

你可以这样做:

private void killOldMap() {
    SupportMapFragment mapFragment = ((SupportMapFragment) getSherlockActivity()
            .getSupportFragmentManager().findFragmentById(R.id.map));

    if(mapFragment != null) {
        FragmentManager fM = getFragmentManager();
        fM.beginTransaction().remove(mapFragment).commit();
    }

}

然后从onDetach()onDestroyView() 调用它以确保旧地图被杀死。

如您所见,这是一种 hack,您最好只以编程方式制作您的应用程序,而不是使用 XML。

【讨论】:

  • 谢谢!我在这上面花了几个小时。
  • 这会导致 IllegalStateException,因为我们会在调用 onSaveInstance() 之后尝试更改片段的状态! hack 不起作用!
【解决方案2】:

试试这个...

从 onDetach() 和 onDestroyView() 调用这个函数来杀死存在的片段事务。

private void commitMaps() {
    if (null != mapFragment) {
        getFragmentManager().beginTransaction().remove(mapFragment).commitAllowingStateLoss();
    }
}

如果你 commit() 存在映射事务,有时你会得到“IllegalStateException: Can't perform this action after onSaveInstanceState”。为避免此异常,您应该在删除现有事务时使用 commitAllowingStateLoss()。

commit() and commitAllowingStateLoss()之间的区别

【讨论】:

    【解决方案3】:

    确保您的活动扩展片段活动并替换

    map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    

    并删除“android:layout_below="@+id/tv_location" because it is inside relative layout

    去吧,让我知道。

    【讨论】:

      【解决方案4】:

      已经创建了一个地图片段,当您尝试返回地图页面时,它会尝试在其顶部创建另一个片段。下面的代码对我来说终于可以正常工作了:

      XML:

      <fragment
              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:id="@+id/map"
              android:name="com.google.android.gms.maps.SupportMapFragment"/>

      请注意,我使用的是 SupportMapFragment 而不是 MapFragment。

      JAVA:

       @Override
      public void onDestroyView() {
          super.onDestroyView();
          Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
          if (f != null)
              getFragmentManager().beginTransaction().remove(f).commit();
      }
      

      【讨论】:

        【解决方案5】:

        我在这里看到的第一个问题是您提供的代码是这一行:

        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapp)).getMap();
        

        您尝试使用通常的FragmentManager 获取SupportMapFragment 对象。这是错误的。 要获取SupportMapFragment 对象,您必须像这样使用SupportFragmentManager

        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapp)).getMap();
        

        但我的猜测是,您现在遇到的问题还不是源于这个错误。 我认为您以错误的方式引用了google-support-v4 库的google-play-services 库。

        您可以查看我写的这篇关于如何将Google Map API V2 添加到您的应用程序的博文:

        Google Map API V2

        更新:

        如果您的最小 SDK 为 11,那么您可以将片段更改为以下代码:

        <fragment
                android:id="@+id/mapp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/tv_location"
                android:name="com.google.android.gms.maps.MapFragment" />
        

        【讨论】:

        • 但是,Emil 在这里我正在扩展片段,所以我不能使用 getSupportFragmentManager()。
        • 然后获取 MapFragment 的实例而不是 SupportMapFragment。
        • 您正在编写应用程序的最小 sdk 是多少?
        • 检查this
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多