二进制XML文件行#9:错误充气类片段与谷歌地图碎片碎片、片段、错误、地图

2023-09-03 22:43:26 作者:给朕来包辣条

该地图显示了完全当我第一次选择标签片段,但是如果我浏览到其他选项卡,并重新选择地图片段选项卡它会抛出我这个错误 - 二进制XML文件中的行#9:错误充气类片段。我看对其他类似的线程,他们中的一些建议,我的类应该扩展片段的活性,而不是我不想这样做。

这是我MapActivity.java:

 公共类MapActivity扩展片段实现ActionBar.TabListener {
私人片段mFragment;
私人GoogleMap的地图;
私人浏览视图;


@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    如果(查看!= NULL){
        ViewGroup中父=(ViewGroup中)view.getParent();
        如果(父!= NULL)
            parent.removeView(视图);
    }
    尝试 {
        鉴于= inflater.inflate(R.layout.activity_map,集装箱,假);
        MapsInitializer.initialize(view.getContext());
    }赶上(InflateException EXP){
        exp.printStackTrace();
        / *地图已经存在,只是返回观点,因为它是* /
    }赶上(GooglePlayServicesNotAvailableException E){
        e.printStackTrace();
    }

    地图=((MapFragment)getFragmentManager()。findFragmentById(R.id.map))
            .getMap();

    返回查看;
}

@覆盖
公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
    mFragment =新MapActivity();
    fragmentTransaction.replace(android.R.id.content,mFragment);
}

@覆盖
公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
    fragmentTransaction.remove(mFragment);
    如果(httpAsyncTask!= NULL)
        httpAsyncTask.setCanceled(真正的);
}

@覆盖
公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){

}
}
 

这是我的xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:weightSum =100
工具:上下文=MapActivity。>

<片段
    机器人:ID =@ + ID /图
    机器人:layout_width =match_parent
    机器人:layout_height =0dp
    机器人:layout_weight =90
    类=com.google.android.gms.maps.MapFragment/>

<按钮
    机器人:ID =@ + ID / map_get_direction
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =0dp
    机器人:layout_weight =10
    机器人:背景=@可绘制/ getdirection_button/>

< / LinearLayout中>
 
多个二进制分析工具各项性能进行的分析与比较

解决方案

该错误可能会说,它不能夸大,因为实体已经存在?如果你想要做这种方式,你需要删除引用到地图时ü离开,以便它能够被再次添加标签。

例如调用一个方法:

 公共无效清除(){
    。getFragmentManager()的BeginTransaction()删除(图).commit()。
}
 

的方式,我建议你这样做是你延长mapfragment而不是片段,那么你就不再需要一个布局文件,并没有额外的招数都需要

The map shows up perfectly when I first select the tab fragment, however if I navigate to other tab and reselect the map fragment tab it will throws me this error - "Binary XML file line #9: Error inflating class fragment". I have look on other similar threads, some of them suggesting that my class should extend fragment activity instead and I don't want to do that.

This is my MapActivity.java:

public class MapActivity extends Fragment implements ActionBar.TabListener{
private Fragment mFragment;
private GoogleMap map;
private View view;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.activity_map, container, false);
        MapsInitializer.initialize(view.getContext());
    } catch (InflateException exp) {
        exp.printStackTrace();
        /* map is already there, just return view as it is */
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

    return view;
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    mFragment = new MapActivity();
    fragmentTransaction.replace(android.R.id.content, mFragment);
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    fragmentTransaction.remove(mFragment);
    if(httpAsyncTask!=null)
        httpAsyncTask.setCanceled(true);
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

}
}

This is my xml :

<LinearLayout 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:orientation="vertical"
android:weightSum="100"
tools:context=".MapActivity" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="90"
    class="com.google.android.gms.maps.MapFragment" />

<Button
    android:id="@+id/map_get_direction"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="10"
    android:background="@drawable/getdirection_button" />

</LinearLayout>

解决方案

The error probably says that it can't inflate because an entity already exists? If you want to do it this way you need to remove the reference to the map when u leave the tab in order for it to be able to be added again.

For example call a method:

public void cleanUp(){
    getFragmentManager().beginTransaction().remove(map).commit();
}

The way I suggest you do it is that you extend mapfragment instead of fragment, then you no longer need a layout file and no extra "tricks" are needed