gpt4 book ai didi

android - getSupportFragmentManager 返回 null

转载 作者:行者123 更新时间:2023-11-30 02:55:35 26 4
gpt4 key购买 nike

我想在我的 Android 应用程序中显示 Google map :

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 ...

我的 Activity 的 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>

我的应用 list :

<?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>

最佳答案

问题出在:

setContentView(R.layout.activity_main);

当然可以

setContentView(R.layout.activity_display_map);

关于android - getSupportFragmentManager 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23326872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com