【问题标题】:Android Fragment Google Maps V2: Error inflating classAndroid Fragment Google Maps V2:膨胀类时出错
【发布时间】:2013-10-15 08:57:11
【问题描述】:

我正在开发一个使用片段选项卡的应用程序,我的一个片段使用 Google Maps V2 "SupportMapFragment"

public class A extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
   {
    View view = null;
    view = inflater.inflate(R.layout.all_coffee_shops_view, container, false);
    map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
   }
}

我的 xml:

<RelativeLayout
   android:id="@+id/map_container"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <fragment
     android:id="@+id/map"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     class="com.google.android.gms.maps.SupportMapFragment" 
   />
</RelativeLayout>

我从这个 A 片段(A->B)调用 B 片段,当我从 B-> 回来时,A 片段 onCreateView 抛出异常,“android.view.InflateException: Binary XML file line # 132: 膨胀类片段时出错”

【问题讨论】:

标签: android android-fragments


【解决方案1】:

当地图 fragment 被创建后,当再次回到这个片段时,片段是 onCreateView() 被调用,这就是你强制关闭的原因

在包含地图的片段中尝试一次

@Override
public void onDestroyView() {
    super.onDestroyView();

    Fragment f = getFragmentManager().findFragmentById(R.id.map);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();
}

请评论我的结果

【讨论】:

  • 嘿 NARESH REDDY,感谢您回答我的问题...因为我正在使用片段选项卡,我正在使用基本容器来处理选项卡中的视图层次结构,您的代码不起作用,但是我将 getSupportFragmentManager 用于 getFragmentManager,然后它就像一个魅力......
【解决方案2】:
public void onDestroyView() {
    super.onDestroyView();
    Log.d(TAG, "onDestroyView");

    Fragment f = getActivity()
            .getSupportFragmentManager().findFragmentById(R.id.map);
    if (f != null) {
        getActivity().getSupportFragmentManager()
        .beginTransaction().remove(f).commit();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2023-03-27
    • 1970-01-01
    • 2013-11-27
    相关资源
    最近更新 更多