我正在尝试在片段中显示MapView(使用被黑客入侵的兼容性库).以下在过去工作得很好:
onCreateView()只是返回一个新的FrameLayoutonActivityCreated()从Acitivity获取MapView并将其添加到其视图层次结构中onDestroyView() 从视图层次结构中删除MapView现在我希望片段使用xml中定义的布局,这样我就可以拥有其他一些UI内容.将MapView元素放在布局文件中总是崩溃,所以我这样做:
map_screen_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/map_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我MapScreenActivity保持实际MapView和片段调用getMapView(),所以我不会遇到"不能有多个MapView"的问题:
MapScreenActivity.java
public class MapScreenActivity extends FragmentActivity {
protected Fragment fragment;
protected MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_pane_empty);
if (savedInstanceState == null) {
fragment = new MapScreenFragment();
getSupportFragmentManager().beginTransaction().add(R.id.root_container, fragment)
.commit();
}
}
public MapView getMapView() {
if (mapView == null) …Run Code Online (Sandbox Code Playgroud)