【问题标题】:getSupportFragmentManager returns nullgetSupportFragmentManager 返回 null
【发布时间】:2014-04-27 17:51:58
【问题描述】:

我想在我的 Android 应用中显示 Google 地图:

public class DisplayMapActivity extends ActionBarActivity {
    private LbsTracker2 lbsTracker2;
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            this.initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

    private void initilizeMap() {
        if (googleMap == null) {
            MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); // <--- always null! :(
            googleMap = mapFragment.getMap();
        }
    }
    ...
}

但我的变量 mapFragment 在我的方法 initializeMap() 中始终为 null ...

我的活动 Xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lbsapp.DisplayMapActivity$PlaceholderFragment" >

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

</RelativeLayout>

我的应用程序清单:

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

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

    <uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />

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

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

    <!-- Required to show current location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <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" >
        <activity
            android:name="com.example.lbsapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.lbsapp.DisplayLbsActivity"
            android:label="@string/title_activity_display_lbs"
            android:parentActivityName="com.example.lbsapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.lbsapp.MainActivity" />
        </activity>

        <!-- Goolge Maps API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDnnWGwO8Zfbn5PGKXPF3aspbZTCtJ6j7I" />

        <activity
            android:name="com.example.lbsapp.DisplayMapActivity"
            android:label="@string/title_activity_display_map"
            android:parentActivityName="com.example.lbsapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.lbsapp.MainActivity" />
        </activity>
    </application>    

</manifest>

【问题讨论】:

  • 您的应用在启动时会崩溃吗?你能记录日志吗?
  • 不会因为它在 try/catch 中而崩溃。不需要日志。问题是 (SupportMapFragment)getSupportFragmentManager() .findFragmentById(R.id.map);返回空值。

标签: android google-maps google-maps-android-api-2 google-maps-api-2


【解决方案1】:

问题就在那里:

setContentView(R.layout.activity_main);

当然是这样

setContentView(R.layout.activity_display_map);

【讨论】:

    【解决方案2】:

    我建议使用 sopportMapFragment 来支持旧版本..

    例子:

    <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="tag_fragment_map" />
    
    SupportMapFragmentmapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
    

    【讨论】:

      【解决方案3】:

      我认为如果您使用放入 cmets 的行,它会起作用。取消注释 MapFragment 并使用 SupportMapFragment 注释该 MapFragment。 MapFragment 是地图片段的现代版本。

      另外,在您的布局中尝试使用 class="com.google.android.gms.maps.MapFragment" 而不是 SupportMapFragment。

      【讨论】:

      • 试试我添加到答案中的布局更改。
      • 为什么使用MapFragment 而不是SupportMapFragment
      • 刚刚注意到一个重要的细节。在您的代码中,您在 android:name 属性中设置片段的类型。尝试类,而不是 android: 在它前面。
      • 我试过 MapFragment 和 SupportMapFragment。两者都返回 null。类而不是 android:name 也不起作用。
      • 好吧,根据文档 MapFragment 适用于 Android 3.1 或更高版本 (API 12)。 SupportMapFragment 支持较旧的 Android 版本,因此不太适合新应用程序。在我的实现中,我将最低 SDK 版本设置为 14,并且我始终可以使其与 MapFragment 一起使用。
      猜你喜欢
      • 2018-07-14
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多