首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >第二次打开SupportMapFragment时出错

第二次打开SupportMapFragment时出错
EN

Stack Overflow用户
提问于 2013-01-28 23:34:26
回答 8查看 34.1K关注 0票数 55

当第二次打开我的SupportMapFragment (安卓地图v2) (调用setContentView)时,我得到以下错误:

代码语言:javascript
复制
01-28 16:27:21.374: E/AndroidRuntime(32743): FATAL EXCEPTION: main
01-28 16:27:21.374: E/AndroidRuntime(32743): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.View.inflate(View.java:16119)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at mypackage.MyView.<init>(HitsView.java:26)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at mypackage.MenuListFragment.onItemClick(MenuListFragment.java:133)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.widget.AbsListView$1.run(AbsListView.java:3529)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.os.Handler.handleCallback(Handler.java:615)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.os.Looper.loop(Looper.java:137)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.app.ActivityThread.main(ActivityThread.java:4745)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at java.lang.reflect.Method.invokeNative(Native Method)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at java.lang.reflect.Method.invoke(Method.java:511)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at dalvik.system.NativeStart.main(Native Method)
01-28 16:27:21.374: E/AndroidRuntime(32743): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f04003b, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
01-28 16:27:21.374: E/AndroidRuntime(32743):    ... 20 more

XML文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/hits_map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.google.android.gms.maps.SupportMapFragment"
        map:mapType="normal"
        map:uiZoomControls="false"
        map:uiZoomGestures="true"
        map:cameraZoom="13"
        map:uiRotateGestures="true"
        map:uiTiltGestures="true"/>
</RelativeLayout>

MyView.class:

代码语言:javascript
复制
public class MyView extends RelativeLayout {
    private GoogleMap map;

    public MyView(Context context, FragmentActivity activity) {
        super(context);
        inflate(activity, R.layout.activity_hits, this);
        this.map = ((SupportMapFragment) activity.getSupportFragmentManager()
            .findFragmentById(R.id.hits_map)).getMap();
    }
}

我不知道这个错误是什么意思。有人能解释一下吗?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2013-02-04 23:56:55

不要在布局中声明de SupportMapFragment,而是通过编程来实现,并确保使用getChildFragmentMananger而不是经典的getFragmentManager()来创建片段。

代码语言:javascript
复制
  mMapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction =
             mMapFragment.getChildFragmentManager().beginTransaction();
     fragmentTransaction.add(R.id.map_root, mMapFragment);
     fragmentTransaction.commit(); 

保留此SupportMapFragment mMapFragment,因为您需要它来检索GoogleMap对象:

代码语言:javascript
复制
  GoogleMap map = mMapFragment.getMap();
票数 51
EN

Stack Overflow用户

发布于 2013-09-20 06:05:25

我花了半天的时间来解决这个问题,但只找到了一个解决方法。在YourFragment类中覆盖您的onCreate方法:

代码语言:javascript
复制
public void onCreate(Bundle savedInstanceState) {
    setRetainInstance(true); 
    super.onCreate(savedInstanceState);     
}

并覆盖您的onDestroy方法:

代码语言:javascript
复制
@Override
public void onDestroyView() {
    super.onDestroyView();
    try {
        SupportMapFragment fragment = (SupportMapFragment) getActivity()
                                          .getSupportFragmentManager().findFragmentById(
                                              R.id.multi_inns_on_map);
        if (fragment != null) getFragmentManager().beginTransaction().remove(fragment).commit();

    } catch (IllegalStateException e) {
        //handle this situation because you are necessary will get 
        //an exception here :-(
    }
}
票数 24
EN

Stack Overflow用户

发布于 2014-08-29 09:07:04

我花了一整天寻找这个问题的解决方案,阅读了这里所有的变通方法,我找到了解决问题的方法。我将发布所有的代码,因为这个问题缺少一个完整的答案,只是一些与完整场景匹配的小片段,确实对任何人都有帮助。

代码语言:javascript
复制
public class LocationFragment extends Fragment implements View.OnClickListener {

    private GoogleMap googleMap;
    private static View rootView;
    private SupportMapFragment supportMapFragment;

    public LocationFragment() {
    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(savedInstanceState == null) {

            if (rootView != null) {
                ViewGroup parent = (ViewGroup) rootView.getParent();
                if (parent != null)
                    parent.removeView(rootView);
            }

            try{
                if(rootView == null)
                {
                    rootView = inflater.inflate(R.layout.fragment_location, container, false);
                }

                supportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
                LocationUtility locationUtility = LocationUtility.getInstance(context);
                GoogleMap googleMap =  locationUtility.initilizeMap(supportMapFragment);

              //More code

            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        return rootView;
    }

}

通过对其进行调试,您将注意到,片段如何操作并不重要,您不需要onDestroyMethod的botter,等等,因为null的验证只与对象的当前实例操作。

尽情享受!;)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14565460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档