且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-03 18:03:22

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

例如调用一个方法:

 公共无效清除(){
    。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