0

嗨,伙计们,

我正在尝试使用 SupportMapFragment 将 Google Maps API 加载到我的应用程序中。发生的事情是我尝试使用特定的布局,而不是占据整个屏幕的简单地图。

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/greenish"
    android:orientation="vertical"
    android:id="@+id/location_stuff" >

    <RelativeLayout
        android:id="@+id/location_header"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.7"
        android:background="@color/greenish" >

        <RelativeLayout
            android:id="@+id/cancel_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:background="@drawable/done_rounded_btn"
            android:gravity="center" >

            <TextView
                android:id="@+id/retake_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:paddingLeft="7dp"
                android:paddingRight="7dp"
                android:text="@string/cancel"
                android:textAllCaps="true"
                android:textColor="#FFFFFF"
                android:textSize="16sp" />
        </RelativeLayout>

        <TextView
            android:id="@+id/location_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:text="@string/location_map"
            android:textAllCaps="true"
            android:textColor="@color/done_color"
            android:textSize="16sp" />

    </RelativeLayout>

    <fragment
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5.6"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        class="com.google.android.gms.maps.LocationFragment" />

    <RelativeLayout
        android:id="@+id/save_location_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/greenish" >

        <LinearLayout
            android:id="@+id/delete_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_centerVertical="true"
            android:background="@drawable/done_rounded_btn"
            android:orientation="horizontal"
            android:paddingBottom="7dip"
            android:paddingTop="7dip"
            android:weightSum="7" >

            <TextView
                android:id="@+id/delete_txt"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                android:background="@null"
                android:paddingLeft="10dp"
                android:text="@string/save_location"
                android:textColor="@color/white"
                android:textAllCaps="true"
                android:textSize="18sp" />

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:contentDescription="@string/hello_world"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:src="@drawable/input_map_white" />
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

我已经通过扩展创建了我的特定 SupportMapFragment 类,如下所示:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.captoom.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class LocationFragment extends SupportMapFragment {

    private GoogleMap map=null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.preview_location, container, false);

        SupportMapFragment fragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapview);
        setMapp(fragment.getMap());
        return v;
    }

    public GoogleMap getMapp() {
        return map;
    }

    public void setMapp(GoogleMap map) {
        this.map = map;
    }
}

当我尝试将片段添加到我当前的活动时,我收到一个 XML 膨胀错误。

我尝试像这样添加片段:

LocationFragment mMapFragment = new LocationFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(mMapFragment,null);
fragmentTransaction.commit();

这是错误日志:

07-30 17:57:24.144: E/AndroidRuntime(8231): FATAL EXCEPTION: main
07-30 17:57:24.144: E/AndroidRuntime(8231): android.view.InflateException: Binary XML file line #55: Error inflating class fragment
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at ro.gebs.captoom.fragments.LocationFragment.onCreateView(LocationFragment.java:19)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:299)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at ro.gebs.captoom.fragments.LocationFragment.onCreateView(LocationFragment.java:19)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.os.Handler.handleCallback(Handler.java:615)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.os.Looper.loop(Looper.java:137)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.app.ActivityThread.main(ActivityThread.java:4921)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at java.lang.reflect.Method.invoke(Method.java:511)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at dalvik.system.NativeStart.main(Native Method)
07-30 17:57:24.144: E/AndroidRuntime(8231): Caused by: java.lang.IllegalArgumentException: Binary XML file line #55: Duplicate id 0x7f06009a, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.LocationFragment
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
07-30 17:57:24.144: E/AndroidRuntime(8231):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)

任何形式的帮助都非常感谢。

谢谢!

4

1 回答 1

2

如果您删除onDestroyView() 中的所有嵌套片段,您可以解决此问题。

 public void onDestroyView() {
        super.onDestroyView(); 
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
}

并像往常一样在 onCreateView() 中膨胀它们

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.map, container, false);
}
于 2013-07-30T15:14:33.023 回答