Android - 错误显示第二次打开Goog​​le地图v2或SupportMapFragment

时间:2013-09-05 14:58:36

标签: android android-maps android-maps-v2 supportmapfragment

在我的应用程序中,我在片段中使用Google地图版本2。我的应用程序选项卡主机与片段(共四个选项卡)。地图用法在Tab1-> Searchome(片段类) - > fragment2(片段类) - > SearchResultMap(片段类)。

我的问题是我第一次访问SearchResultMap类时它显示地图但是第二次它与Caused by: java.lang.IllegalArgumentException: Binary XML file line #62: Duplicate id 0x7f050043, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment

崩溃

布局xml的完整代码:

SearchResultMap.java:

public class SearchResultMap extends Fragment {

    private GoogleMap mMap;

    private View fragmentView;

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

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

        fragmentView = inflater.inflate(R.layout.map_layout, container, false);

        FragmentManager myFM = getActivity().getSupportFragmentManager();

        final SupportMapFragment myMAPF = (SupportMapFragment) myFM
                .findFragmentById(R.id.map);

        mMap = myMAPF.getMap();

        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(xxxxxx, xxxxxxxx))
                .title("Current Location")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ball_pointer))
                .snippet("Accuracyt to 20 meters"));


        return fragmentView;
    }

}

map_layout.xml:

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#333333" >

        <ImageButton
            android:id="@+id/map_back"
            android:layout_width="25dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:background="@drawable/back_button" />

        <TextView
            android:id="@+id/search_txt"
            android:layout_width="170dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/map_back"
            android:background="@drawable/rounded_corner"
            android:hint="Search"
            android:textColor="#000000" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/search_txt"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/search_txt" >

            <Button
                android:id="@+id/search_go"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="13sp"
                android:text="Search" />

            <Button
                android:id="@+id/search_cancel"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                 android:textSize="13sp"
                android:text="Cancel" />
        </LinearLayout>
    </RelativeLayout>

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

</LinearLayout>

完整日志猫:

FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #62: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at com.fssd.spot.search.SearchResultMap.onCreateView(SearchResultMap.java:35)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1492)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1280)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #62: Duplicate id 0x7f050043, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

1 个答案:

答案 0 :(得分:0)

我想我昨天解决了完全相同的问题(片段中的嵌套映射V2)。

我在这里找到了一个解决方案: Error opening SupportMapFragment for second time

=&GT;答案是“Kaleb”。以编程方式创建片段(不要将其放在布局中)并查看方法中的代码:onCreateView / onActivityCreated / onResume。

为我工作,希望这可以帮到你。