我有一个片段活动,可以在单击按钮时切换片段。
我切换的两个片段是:
- 谷歌地图 android v2 片段
- 另一个带有文本视图的片段
这就是我切换片段的方式:
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
if(tv.getText().toString().equalsIgnoreCase("Click ME!!")){
tv.setText("MAP");
if (fm != null) {
if(map == null){
map = new Map();
}
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.lay_contaier, map);
ft.commit();
}
}
else{
tv.setText("Click ME!!");
if (fm != null ){
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.lay_contaier,new empty());
ft.commit();
}
}
}
这是我的 Maps Fagment 代码
public class Map extends Fragment implements OnMarkerClickListener, OnInfoWindowClickListener, OnMarkerDragListener {
private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private GoogleMap mMap;
private Marker mPerth;
private Marker mSydney;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main,null);
return view;
}
活动邮件.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"/>
但是每次我切换回地图片段时,应用程序都会崩溃。我应该如何使这段代码正确?