地图片段中的错误与表格形式

时间:2014-11-12 12:28:22

标签: android android-fragments map fragment

我在标签视图中使用地图片段,在第一个和第二个标签中我显示列表视图但在最后一个标签中使用地图片段。当我第一次点击最后一个标签时它显示正确的地图视图但是在第二次应用程序崩溃。请让我知道如何处理。 错误是

11-12 17:53:26.794: E/AndroidRuntime(10174): FATAL EXCEPTION: main
11-12 17:53:26.794: E/AndroidRuntime(10174): android.view.InflateException: Binary XML file line #118: Error inflating class fragment
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at competent.groove.thegroove.partypartner.Party_Partner_Confirm_Fragment.onCreateView(Party_Partner_Confirm_Fragment.java:62)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1291)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.os.Handler.handleCallback(Handler.java:605)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.os.Looper.loop(Looper.java:137)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.app.ActivityThread.main(ActivityThread.java:4456)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at java.lang.reflect.Method.invoke(Method.java:511)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at dalvik.system.NativeStart.main(Native Method)
11-12 17:53:26.794: E/AndroidRuntime(10174): Caused by: java.lang.IllegalArgumentException: Binary XML file line #118: Duplicate id 0x7f060208, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.app.Activity.onCreateView(Activity.java:4248)
11-12 17:53:26.794: E/AndroidRuntime(10174):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
11-12 17:53:26.794: E/AndroidRuntime(10174):    ... 20 more

布局是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" > 

        <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.google.android.gms.maps.MapFragment" >
    </fragment>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

做一件事,在你的xml文件中只添加一个LinearLayout而不是Map Fragment,如下所示:

<LinearLayout
        android:id="@+id/lnrMap"
        android:layout_width="match_parent"
        android:layout_height="Wrap_content"
        android:orientation="vertical"
        android:visibility="visible" >

然后在你的片段中:

private LinearLayout lnrMap;
private SupportMapFragment mapFragment;
private Timer t = null;

    lnrMap = (LinearLayout)fragmentView.findViewById(R.id.lnrMap);
    mapFragment = new SupportMapFragment();
    addFragment(lnrMap.getId(), mapFragment);
    t = new Timer();
                t.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        getActivity().runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                try {

                                    googleMap = mapFragment.getExtendedMap();

                                    mapFragment.getView().setClickable(false);
                                    if (googleMap != null) {
                                        googleMap.getUiSettings().setZoomControlsEnabled(false);
                                        googleMap.getUiSettings().setCompassEnabled(false);
                                        googleMap.getUiSettings().setZoomGesturesEnabled(false);
                                        googleMap.getUiSettings().setScrollGesturesEnabled(false);
                                        googleMap.getUiSettings().setRotateGesturesEnabled(false);
                                        googleMap.getUiSettings().setTiltGesturesEnabled(false);
                                        googleMap.animateCamera(CameraUpdateFactory.zoomTo(6));

                                        t.cancel();
                                    }
                                } catch (Exception e) {
                                }

                            }
                        });

                    }
                }, 0, 500);


//addFragment() method:

public void addFragment(int layoutId, Fragment fragment) {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(layoutId, fragment);
        ft.commit();
    }