【问题标题】:Extending SupportMapFragment shows an all black screen when the fragment is loaded加载片段时扩展 SupportMapFragment 显示全黑屏
【发布时间】:2013-03-30 03:27:08
【问题描述】:

我正在转换一个程序以使用 Fragments,所以它现在有一个实际的 Activity,而其他一切都是一种或另一种类型的 Fragment。

所以 ErrandMapActivity 的层次结构是:

Object -> Fragment -> SupportMapFragment -> MapViewActivity -> ErrandMapActivity

对于 ErrandAssistantFreeMainActivity:

Object -> Context -> ContextWrapper -> ContextThemeWrapper -> Activity -> FragmentActivity -> ErrandAssistantMainActivity -> ErrandAssistantFreeMainActivity

所以活动加载了主要的fragmentlayout,然后加载了mapview,我看到了fragment中的textview,但地图不可见。

对于 ErrandMapActivity,它是一个 Fragment,它会加载这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="com.jblack.android.errandrouter.activity.ErrandMapActivity"
    android:orientation="vertical"
    map:mapType="normal"
    map:uiCompass="true"
    map:uiRotateGestures="true"
    map:uiScrollGestures="true"
    map:uiTiltGestures="true"
    map:uiZoomControls="true"
    map:uiZoomGestures="true" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

ErrandMapActivity我有这个方法:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mContext = getActivity().getApplicationContext();
    super.onCreateView(inflater, container, savedInstanceState);
    view = inflater.inflate(R.layout.map_fragment_layout, container,
            false);
    return view;
}

MapViewActivity.onResume 我有 mMapView = getMap() 并且我有这些值,以验证 SupportMapFragment 是否实际实例化了一个地图:

mMapView = GoogleMap (id=830023276792)
  fQ = IGoogleMapDelegate$a$a (id=830023235664)
     b = ag (id=830023221320)
  fR = null

此应用程序中的一个 Activity 使用此布局,因此加载此布局然后实例化地图片段:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/allFragmentsFrameLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.jblack.android.errandrouter.activity.ErrandMapActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

所以,我知道我的扩展 SupportMapFragment 的片段正在被加载,那么为了加载地图可能需要做什么?

【问题讨论】:

  • 这段代码非常混乱。您有两个认为它们是活动的片段(MapViewActivityErrandMapActivity),它们之间的关系不清楚,并且您没有活动(尽管有多个名为 ...Activity 的事物)。因此,我不知道您要做什么,更不用说为什么它不起作用了。使用层次结构视图查看您的 MapView 去了哪里。
  • @CommonsWare - 我正在修改一个程序以使用片段,但不想更改所有名称。这就是为什么它们被命名为活动。
  • @JamesBlack 似乎是一种不好的做法。您(或一些不幸的同事)将来必须保持这种状态。你正在制造债务。

标签: android google-maps android-fragments


【解决方案1】:

你摆脱了你的地图。

ErrandMapActivity 覆盖 onCreateView() 并丢弃地图(从超类获得),将其替换为 LinearLayoutTextView

您不必这样做。 :-)

例如,这可能有效:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // one seriously hopes that you never use mContext = getActivity().getApplicationContext();
    view = inflater.inflate(R.layout.map_fragment_layout, container,
            false);

    View whatYourParentGaveYou=super.onCreateView(inflater, view, savedInstanceState);

    view.addView(whatYourParentGaveYou, /* insert valid LinearLayout.LayoutParams here */

    return view;
}

LinearLayout 上的所有 map 属性很可能会被忽略,因此请通过 Java 代码进行设置。

【讨论】:

  • 谢谢,我今天试试。
  • 我只是直接使用了 SupportMapFragment,它按预期工作,所以我将编写一个新片段来与地图交互,这可能是一个更好的设计。
【解决方案2】:

我想通了,因为它是显示的活动的黑色背景。白色背景的用户自定义主题或任何默认的白色。对于 E.x

@dimen/actionbar_height @android:颜色/白色

并将其设置为清单中的活动。请记住,如果您使用任何对话,这种白色背景会影响背景。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    相关资源
    最近更新 更多