在开发涉及 Android Google Maps API v2.0 的应用程序时,我发现 FragmentManager.findFragmentById() 总是返回 NULL。我从 XML 文件中扩充了片段。我可以得到 View 没问题,但是 Fragment 本身呢?我什至尝试了 4 秒的延迟,以确保在我获得 ID 时所有内容都已加载。
//
// res/layout/mapfragment.xml:
//
<?xml version="1.0" encoding="UTF-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
// Transmitting resource ID of fragment in XML
ui.setMapFragmentResourceID(R.layout.mapfragment);
..
// Showing map
@Override
public void showMap() {
...
m_map = m_currentActivity.getLayoutInflater().inflate(m_mapFragmentResourceID, null);
cv.postDelayed(new Runnable() {
@Override
public void run() {
afterShowMapView();
}
}, 4000);
}
// 4000 msec delay to allow for everything to load
public void afterShowMapView()
{
FragmentActivity fa = (FragmentActivity)m_currentActivity;
FragmentManager fm = fa.getSupportFragmentManager();
SupportMapFragment f = (SupportMapFragment)fm.findFragmentById(m_mapFragmentResourceID);
}
//
// f is always null
//
如何获取 Fragment/SupportFragment 对象本身(与我通过膨胀布局获得的视图相反)?