【问题标题】:Why is returning me NULL when I go to find map with FragmentManager?为什么我用 FragmentManager 查找地图时返回 NULL?
【发布时间】:2014-11-18 18:52:29
【问题描述】:

您好,当我从 Fragment Manager 对象获取地图时,我去查找地图片段,它返回 null,为什么?

我的代码是:

public class MapaFragment extends Fragment  {
...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     
        View v = super.onCreateView(inflater, container, savedInstanceState);

        android.app.FragmentManager fragmentManager = getFragmentManager();
        MapFragment mapFragment = (MapFragment) fragmentManager.findFragmentById(R.id.map);
        GoogleMap map = mapFragment.getMap();

        return v;
    }
 ...
}

mapFragment 中的这段代码为空,显然我无法获取 GoogleMap。 LogCat 在该行中给我 NullPointer 错误。

我的片段是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</LinearLayout>

我认为正确的权限和清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <permission
        android:name="info.android.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="info.android.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="6171000" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
        </activity>            
        <activity
            android:name=".LoginActivity"
            android:label="Login" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ImagenActivity"
            android:label="@string/title_activity_imagen" >
        </activity>
    </application>

</manifest>

我得到这个屏幕:

我正在使用的配置屏幕是:

【问题讨论】:

    标签: android google-maps android-fragments adt gis


    【解决方案1】:

    编辑:您用于测试的设备需要 Google Play 服务。有关设置模拟器的指南,请参阅 this article

    将您的布局的 XML 更改为此,并记下 XML 文件的名称:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map" />
    
    </LinearLayout>
    

    例如,如果文件名是 map_fragment_layout.xml,请将您的 View v 更改为以下内容:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){     
        View v = inflater.inflate(R.layout.map_fragment_layout, null);
    
        MapView mapView = (MapView) v.findViewById(R.id.map);
        GoogleMap map = mapView.getMap();
    
        return v;
    }
    

    另一种选择:扩展 MapFragment
    此选项与前一个选项完全无关。在这种情况下,您无需创建、分配或扩充 XML 文件。视图是以编程方式生成的。

    public static class MapaFragment extends MapFragment {
    
        GoogleMap mMap;
    
        @Override
        public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2){
            View v = super.onCreateView(arg0,arg1,arg2);
            mMap = getMap();
            return v;
        }
    }
    

    【讨论】:

      【解决方案2】:

      编辑:

      我认为您实际上并没有夸大布局,因为您正在调用 super 方法。

      尝试改变

      View v = super.onCreateView(inflater, container, savedInstanceState);
      

      到:

      View v = inflater.inflate(R.id.yourLayout, container, false);
      

      注意:将 R.id.yourLayout 替换为您的片段布局名称。

      【讨论】:

      • @David 哦,我的错。更新了我的答案。
      猜你喜欢
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 2020-03-01
      相关资源
      最近更新 更多