谷歌地图V2 Android错误:膨胀类碎片错误

6

我正在创建一个带有标签页的应用程序,其中一个标签页中有一个地图。当我从中打开地图时,它可以正常工作,当我访问其他标签页时,也可以正常工作。但是,当我回到地图标签页时,应用程序会崩溃,并出现以下错误。

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文件。 - Dhaval Parmar
5个回答

18
您的问题是在XML中声明了Google Maps v2片段,因此每次填充XML时都会创建一个新的片段。但是,片段的ID在XML中是硬编码的,因此它将尝试使用与已运行的片段相同的ID创建新的片段。两个正在运行的片段不能具有相同的ID,因此应用程序会崩溃。
最好的解决方法是通过编程方式制作地图片段。可以在Google Maps示例项目的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来创建您的应用程序。


谢谢!我花了几个小时在这上面。 - anil
2
这会导致IllegalStateException,因为我们在onSaveInstance()被调用后尝试更改片段的状态!这个hack不起作用! - stack_ved

2

试试这个...

在onDetach()和onDestroyView()中调用此函数以终止现有的片段事务。

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

有时候当你执行commit()方法删除一个已存在的map事务时,会出现“IllegalStateException: Can not perform this action after onSaveInstanceState”的异常。为了避免这个异常,你应该使用commitAllowingStateLoss()方法来删除已存在的事务。
commit()和commitAllowingStateLoss()方法的区别:请参考这里

0

已经创建了一个地图片段,当您尝试返回地图页面时,它会在其顶部尝试创建另一个片段。以下代码对我来说最终运行良好:

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();
}

0

请确保您的Activity继承自FragmentActivity并替换

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

移除 "android:layout_below="@+id/tv_location" 因为它在相对布局内"

按照这个做,然后告诉我。


-1
这段代码中我看到的第一个问题是这一行:
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()。 - Sreedhu Madhu
获取MapFragment的实例,而不是SupportMapFragment。 - Emil Adz
你的应用程序是为哪个最低SDK版本编写的? - Emil Adz
请查看这个链接:http://android-er.blogspot.in/2012/12/add-reference-library-google-play.html - Amol Sawant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接