谷歌地图V2安卓错误膨胀类碎片错误

时间:2013-04-09 08:52:06

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

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

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>

5 个答案:

答案 0 :(得分:18)

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

解决此问题的最佳方法是以编程方式制作地图片段。在谷歌地图示例项目的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()调用它以确保旧地图被杀死。

正如您所看到的,这是一种破解,您最好只是以编程方式制作应用而不是使用XML。

答案 1 :(得分:2)

试试这个......

从onDetach()和onDestroyView()调用此函数来杀死现有的片段事务。

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

有时你会得到&#34; IllegalStateException:在onSaveInstanceState之后无法执行此操作&#34;如果你commit()存在map事务。要避免此异常,应在删除现有事务时使用commitAllowingStateLoss()。

commit() and commitAllowingStateLoss()

之间的差异

答案 2 :(得分:0)

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

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

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

跟着这个让我知道。

答案 3 :(得分:0)

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

XML:

&#13;
&#13;
<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"/>
&#13;
&#13;
&#13;

请注意我使用的是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();
}

答案 4 :(得分:-1)

我在这里看到的第一个问题就是这一行:

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

您尝试使用通常的SupportMapFragment获取FragmentManager个对象。这是错的。 要获取SupportMapFragment对象,您必须使用SupportFragmentManager,如下所示:

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

但我的猜测是,你现在遇到的问题还没有从这个错误中得出。 我认为您错误地引用了google-play-services库的google-support-v4库。

您可以查看我撰写的关于如何将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" />
相关问题