谷歌地图V2 Android的错误充气类片段的错误 [英] Google Map V2 android error Inflating class fragment error

查看:238
本文介绍了谷歌地图V2 Android的错误充气类片段的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造与标签的应用程序,我已经映射选项卡中的一个,当我打开地图,从它工作正常,当我访问了一些其他的选项卡的时候也能正常工作,但是当我回来的地图标签应用程序崩溃,此错误。

  04-09 14:10:43.866:E / AndroidRuntime(28184):android.view.InflateException:二进制XML文件中的行#42:错误充气类片段
04-09 14:10:43.866:E / AndroidRuntime(28184):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
04-09 14:10:43.866:E / AndroidRuntime(28184):在android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-09 14:10:43.866:E / AndroidRuntime(28184):在android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-09 14:10:43.866:E / AndroidRuntime(28184):在android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-09 14:10:43.866:E / AndroidRuntime(28184):在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-09 14:10:43.866:E / AndroidRuntime(28184):在com.research.fragmenttabstudy.tabA.TodaysDealLocation.onCreateView(TodaysDealLocation.java:43)
 

下面是我的code:

 公开查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        Log.d(犯错,todaysdeal位置);
        Log.d(犯错,todaysdeal位置+ inflater.toString()++ container.toString()+);
        super.onCreateView(充气,容器,savedInstanceState);
// map.clear();
        查看查看= inflater.inflate(R.layout.todays_deal_location,集装箱,
                假);

        Log.d(犯错,view.toString());
        回到=(ImageView的)view.findViewById(R.id.list);
        back.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){

                // TODO自动生成方法存根
                // Constants.pass code_chk = 0;
                // 完();
                mActivity.popFragments();
            }
        });
        返回查看;
    }
    @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        地图=((SupportMapFragment)getFragmentManager()。findFragmentById(
                R.id.mapp))的GetMap()。
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    }
 

XML:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:SRC =@可绘制/顶栏/>

    <! - < ImageView的 - >
    <  - 机器人:ID =@ + ID / deallistmenu - >
    !<  - 机器人:layout_width =WRAP_CONTENT - >
    !<  - 安卓layout_height =WRAP_CONTENT - >
    <  - 安卓!layout_marginLeft =5DP - >
    !<  - 安卓layout_marginTop =9dp - >
    <  - 机器人:SRC =@可绘制/ deallist_menubtn/> - >

    < ImageView的
        机器人:ID =@ + ID /列表
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_marginRight =7DP
        机器人:layout_marginTop =7DP
        机器人:SRC =@可绘制/ map_list/>

    < RelativeLayout的
        机器人:ID =@ + ID /的FrameLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_below =@ ID / imageView1
        机器人:layout_marginTop = -  4.8dp>

        <! - 框布局........................................... .........................  - >


        <! - 框布局........................................... .........................  - >

        <片段
            机器人:ID =@ + ID / MAPP
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_below =@ + ID / tv_location
            机器人:名称=com.google.android.gms.maps.SupportMapFragment/>

        <! - < ZoomControls  - >
        <  - 机器人:ID =@ + ID / zoomControls - >
        !<  - 机器人:layout_width =WRAP_CONTENT - >
        !<  - 安卓layout_height =WRAP_CONTENT - >
        !<  - 安卓layout_alignParentBottom =真 - >
        <  - 安卓!layout_alignParentLeft =真 - >
        !<  - 安卓layout_marginBottom =30dp - >
        !<  - 安卓layout_marginLeft =100dp/> - >

    < / RelativeLayout的>

    <的TextView
        机器人:ID =@ + ID / barTitle
        机器人:layout_width =match_parent
        机器人:layout_height =50dp
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:重力=中心
        机器人:行=1
        机器人:MAXLINES =1
        机器人:以下属性来=50dip
        机器人:paddingRight =55dip
        机器人:单线=真
        机器人:文本=
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:文字颜色=@机器人:彩色/白
        机器人:TEXTSIZE =18dp/>

< / RelativeLayout的>
 

解决方案

您的问题是,你已经宣布谷歌地图V2片段XML,所以每次充气它使一个新片段的XML。然而,该片段的id是硬codeD中的XML,所以它会尝试用相同的ID为已经运行一个创建新的片段。两个运行片段不能有相同的ID,所以应用程序崩溃。

解决这个问题的最好办法是使地图片段编程。这方面的一个很好的例子可以在谷歌地图为例项目ProgrammaticDemoActivity.java一部分被发现。

但是,如果由于某种原因,你必须声明你的片段在XML中,你可以把它杀死旧的地图碎片,当你离开的视图中工作,因此可以创建新的。

您可以这样做:

 私人无效killOldMap(){
    SupportMapFragment mapFragment =((SupportMapFragment)getSherlockActivity()
            。.getSupportFragmentManager()findFragmentById(R.id.map));

    如果(mapFragment!= NULL){
        FragmentManager FM = getFragmentManager();
        。fM.beginTransaction()删除(mapFragment).commit();
    }

}
 

,然后从 onDetach称之为() onDestroyView(),以确保旧地图被杀害。

正如你所看到的,这是怎样的一个黑客,你是最好的关闭只是编程让您的应用程序,而不是使用XML。

I am creating an application with tab and I have map in one of the tab, when I open map from it works fine and when I visit some other tab that time also it works fine, but when I come back to the map tab app crashes with this error.

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)

Here is my code:

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>

解决方案

Your problem is that you have declared the google maps v2 fragment in XML, so every time you inflate the XML it makes a new fragment. However, the id of the fragment is hard-coded in the XML, so it will try to create the new fragment with the same id as the one that is already running. Two running fragments cant have the same id, so the app crashes.

The best way to fix this is to make the map fragment programmatically. A good example of this can be found in ProgrammaticDemoActivity.java part of the google maps example project.

However, if for some reason you must declare your fragment in XML, you can make it work by killing the old map fragment when you leave the view, so the new one can be created.

You can do something like:

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

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

}

and then call it from onDetach() and onDestroyView() to ensure the old map is killed.

As you can see, this is kind of a hack and you are best off just programmatically making your app instead of using XML.

这篇关于谷歌地图V2 Android的错误充气类片段的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆