发生了什么:: 我无法找到将此行fm.findFragmentById(R.id.mySupportMapId)
返回为null的地图视图
:
FragmentManager fm=getActivity().getSupportFragmentManager();
SupportMapFragment smf = (SupportMapFragment) fm.findFragmentById(R.id.mySupportMapId);
googleMap=smf.getMap();
in xml
<FrameLayout
android:id="@+id/rootJobsLocationId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<fragment
android:id="@+id/mySupportMapId"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
other code
smf变为空
注意: 我有一个设备有谷歌播放服务....当我使用regularfragment而不是支持片段时,这个错误不存在
答案 0 :(得分:1)
试试这种方式
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iqamah_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
// Java代码
public class xxxx extends FragmentActivity {
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xxx);
map =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.iqamah_map)).getMap();
}
}
答案 1 :(得分:1)
使用此
(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mySupportMapId)).getMap();
SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = smf.getMap();
而不是
FragmentManager fm=getActivity().getSupportFragmentManager();
SupportMapFragment smf = (SupportMapFragment) fm.findFragmentById(R.id.mySupportMapId);
googleMap=smf.getMap();
答案 2 :(得分:0)
在java文件中试用此代码,
首先,YourActivity是扩展FragmentActivity或ActionBarActivity
将此代码放在Fragment java文件onCreateView()方法
上 SupportMapFragment googleMap = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview);
还要破坏片段上的mapview,
@Override
public void onDestroyView() {
super.onDestroyView();
SupportMapFragment suMapFrag = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapview);
if (suMapFrag != null)
getSupportFragmentManager().beginTransaction().remove(suMapFrag ).commit();
}
我希望这段代码能正常工作...... !!!