无法找到Android谷歌地图

时间:2014-11-07 05:26:44

标签: android google-maps

我有一个实际在应用程序上显示地图的布局

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/fragment_create_instant_album_create_button_text_view" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical"
            android:paddingLeft="30dp"
            android:paddingRight="30dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:text="@string/instant_album_info"
                android:textColor="@color/pico_clay" />

            <EditText
                android:id="@+id/fragment_create_instant_album_album_name_text_view"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/blue_border_edittext_unselected"
                android:hint="@string/album_name"
                android:paddingLeft="15dp"
                android:textColor="@color/pico_dark_grey"
                android:textColorHint="@color/pico_grey_blue"
                android:textSize="@dimen/text_medium" />

            <fragment
                android:id="@+id/fragment_create_instant_album_map_fragment"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_marginTop="15dp"
                class="com.google.android.gms.maps.SupportMapFragment"
                android:background="@drawable/blue_border_edittext_unselected"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:tag="map" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:gravity="center_vertical"
                android:paddingTop="8dp" >

                <RadioGroup
                    android:id="@+id/fragment_create_instant_album_privacy_radio_group"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
                </RadioGroup>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

    <com.coapps.pico.fonts.RobotoTextView
        android:id="@+id/fragment_create_instant_album_create_button_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/pico_salmon"
        android:gravity="center"
        android:paddingBottom="20dp"
        android:paddingTop="20dp"
        android:text="@string/create_instant_album"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:textColor="@android:color/white" />

</RelativeLayout>

但是当使用此代码获取地图片段时,它总是返回null(虽然我看到了地图)

albumName = (TextView) view
        .findViewById(R.id.fragment_create_instant_album_album_name_text_view);
createAlbum = (TextView) view
        .findViewById(R.id.fragment_create_instant_album_create_button_text_view);
mapFragment = (SupportMapFragment) mainActivity.getSupportFragmentManager().findFragmentByTag("map");

为什么呢?我做错了什么?

2 个答案:

答案 0 :(得分:1)

尝试此操作以获取地图

GoogleMap mMap = ((SupportMapFragment) getActivity()
                .getSupportFragmentManager().findFragmentById(R.id.fragment_create_instant_album_map_fragment))
                .getMap();

如果这仍然为null,则尝试此替换。首先,使用此FrameLayout替换xml中的地图片段

 <FrameLayout
    android:id="@+id/fl_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
     />

然后在你的片段的onActivityCreated中,像这样创建你的SupportMapFragment

     SupportMapFragment fragment= (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.ll_map);

    if (fragment == null) {
        fragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.fl_map, fragment).commit();
    }

    GoogleMap mMap=fragment.getMap();

以及你的片段的onDetach(),把这段代码

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

    try {
        Field childFragmentManager = Fragment.class
                .getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

答案 1 :(得分:0)

您是否在清单文件和互联网权限中添加了元数据

  <uses-permission android:name="android.permission.INTERNET" />

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="******2wuOppWIMVpr8DF6u6ydKf********" />

您可以查看此链接http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/