【发布时间】:2014-10-13 05:24:43
【问题描述】:
我正在尝试在片段中加载谷歌地图。问题是当我调用获取 GoogleMap 对象时,对 findFragmentById 的调用总是返回 null。我已经查看了那里的每个帖子并尝试了几乎所有帖子,但无法弄清楚。
我的应用结构是这样的: 1. 我有一个扩展 ActionBarActivity 的 Activity (MINMainActivity)。 2. 在 onCreate 中我调用如下:
mMainView = getLayoutInflater().inflate(R.layout.mimaster_drawer_layout, null);
mimaster_drawer_layout 的来源是:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/init_background">
</FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/black"
android:dividerHeight="1dp"
android:background="@android:color/darker_gray"/>
我用片段的动态列表切换出 FrameLayout (content_frame) 的内容。其中一个片段包含一个 SupportMapFragment。类定义为:
public class MINPageTypeMappingFragment extends Fragment
onCreateView 代码为:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate view
//pageView = inflater.inflate(R.layout.page_mapping_fragment, container, false);
pageView = inflater.inflate(R.layout.page_mapping_fragment, container, false);
// Set up specific controls/views
SetupMapIfNeeded();
return pageView;
}
SetupMapIfNeeded 的代码是:
private void setUpMapIfNeeded()
{
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
SupportMapFragment mapfrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
if(mapfrag != null)
{
mMap = mapfrag.getMap();
}
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
page_mapping_fragment.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"
tools:context=".MINMainActivity" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
编辑:我知道该类应该等于 class="com.google.android.gms.maps.SupportMapFragment" 但是当我这样做时,它会在 onCreateView 的初始扩展期间崩溃。
我还包含了清单文件的相关部分:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
android:name="com.mobilityinitiative.ironman.MINApplication">
<!-- Mobility Initiative Activities -->
<activity android:name=".MINMainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="@string/title_minpagedefinition_list" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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="@string/google_maps_key" />
</application>
就像我说的,让人悲伤的一句话是:
// Try to obtain the map from the SupportMapFragment.
SupportMapFragment mapfrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
mapFrag 始终为空。我是个菜鸟,所以请原谅我的无知。我还使用以下行获得了片段列表:
List<Fragment> frags = getActivity().getSupportFragmentManager().getFragments();
这并没有按预期返回。我在列表中有 3 个片段: 1) MINPageTypeMappingFragment 2) RetainFragment - 来自 ImageCache 的片段 - 不相关 3) MINPageTypeGridFragment - 上一个被 MINPageTypeMappingFragment 替换的片段
我没有看到我预期的“SupportMapFragment”。
我能找到的大多数示例代码都需要从 FragmentActivity 加载 SupportMapFragment。在我的情况下,包含 GoogleMap 的片段是从 MINMainActivity 加载的(基于按钮事件):
pageFragment = new MINPageTypeMappingFragment();
pageFragment.setArguments(arguments);
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.content_frame, pageFragment);
ft.commit();
求助!!!!!!
编辑:
我知道该类应该等于 class="com.google.android.gms.maps.SupportMapFragment" 但是当我这样做时,它会在 onCreateView 的初始扩展期间崩溃: pageView = inflater.inflate(R .layout.page_mapping_fragment,容器,假);我得到的错误是:“InflateException:二进制 XML 文件第 7 行:膨胀类片段时出错。”
【问题讨论】:
-
您好!我遇到了同样的问题。如果您在另一个片段中使用 mapFragment,请尝试使用 getChildFragmentManager() 而不是 getSupportFragmentManager()。
标签: android google-maps android-fragments