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

Sre*_*dhu 6 android google-maps android-fragments layout-inflater android-fragmentactivity

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

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)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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

    }
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

小智 18

你的问题是你已经用XML声明了谷歌地图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();
    }

}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这会导致IllegalStateException,因为我们会在调用onSaveInstance()之后尝试更改片段的状态!黑客行不通! (2认同)

Emi*_*Adz -1

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

map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapp)).getMap();
Run Code Online (Sandbox Code Playgroud)

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

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapp)).getMap();
Run Code Online (Sandbox Code Playgroud)

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

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

谷歌地图 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" />
Run Code Online (Sandbox Code Playgroud)