【问题标题】:ViewPager with Google Map API v2 goes black带有 Google Map API v2 的 ViewPager 变黑
【发布时间】:2014-01-17 07:29:06
【问题描述】:

我已在视图寻呼机中集成了新的 google maps api v2 片段。当我从地图选项卡(街景屏幕)移动到第二个选项卡(客户端详细信息)时,显示的黑色视图将数据插入片段。有人知道解决方案吗??

截图:

代码:

ListingStreetViewFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



        View inflatedView = inflater.inflate(R.layout.map_view, container, false);


        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
        }

        markers = new Hashtable<String, String>();

        mMapView = (MapView) inflatedView.findViewById(R.id.map);
        mMapView.onCreate(mBundle);
        mMapView.onResume();
        setUpMapIfNeeded(inflatedView);

        return inflatedView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
    }

    // mapping between map object with xml layout.
    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
            if (mMap != null) {
                mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
                setUpMap();

            }
        }
    }

    //set location on map and show marker on random location
    private void setUpMap() {
            // random latitude and logitude

            // Adding a marker
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(53.558, 9.927))
                    .title("Hello Maps ");

            marker.snippet("dinet");



            // changing marker color

                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));


            mMap.addMarker(marker);

            // Move the camera to last position with a zoom level
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(53.558,
                                9.927)).zoom(16).build();

                mMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));
        }



    @Override
    public void onResume() {

        if (null != mMapView)
            mMapView.onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != mMapView)
            mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mMapView)
            mMapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (null != mMapView)
            mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (null != mMapView)
            mMapView.onLowMemory();
    }

ListingClientDetailFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.client_detail, container, false);

    initUi(rootView);
    idval = ListingListFragment.cid;
    System.out.println("SET: " + idval);
    setValues();

    return rootView;
}


protected void initUi(View v){
    db = new Databasehelper(getActivity());

    cDetail = new HashMap<String, String>();

    tvName = (TextView)v.findViewById(R.id.tvName);
    tvType = (TextView)v.findViewById(R.id.tvType);
    tvStatus = (TextView)v.findViewById(R.id.tvStatus);
    tvEmail = (TextView)v.findViewById(R.id.tvEmail);
    tvPhone = (TextView)v.findViewById(R.id.tvPhone);

    ivCall = (ImageView)v.findViewById(R.id.ivCall);
    ivMsg = (ImageView)v.findViewById(R.id.ivMsg);

}

//get data from database and set into controls.
protected void setValues(){

    cDetail = db.getClientDetail(idval);
    tvName.setText(cDetail.get("name"));
    tvType.setText(cDetail.get("type"));
    tvStatus.setText(cDetail.get("status"));
    tvEmail.setText(cDetail.get("email"));
    tvPhone.setText(cDetail.get("phone"));

}

logcat 中没有显示错误。所以,我无法从那里追踪它。

【问题讨论】:

  • 请添加一些代码或查看log cat 这可能是因为api key的身份验证错误。
  • @GrIsHu 现在检查..
  • @Rajiv 现在看看我的代码..
  • @Segi 您需要在onCreateView() 类的onCreateView() 方法中使用getMap() 方法获取地图。将线路改为mMapView = (MapView) inflatedView.findViewById(R.id.map).getMap(); 也不需要调用oncreate()onResume()。它会被自动调用。
  • @GrIs 我已经尝试过使用 getMap()。仍然没有工作..

标签: android android-viewpager android-maps-v2


【解决方案1】:

我在一段时间(我相信是四到五个月)前遇到了类似的问题。我有一个从屏幕左侧打开的滑动菜单,以及一个带有地图的片段。当地图显示时,我打开菜单,实际地图向右滑动,但菜单被黑色覆盖层覆盖,地图不知何故明显留在了那里。

我想我已经解决了这个问题,这要归功于对 Stack Overflow 问题的一些开明的回答,但我再也找不到它了。不过,我不确定。也许这是对 Android 错误报告的一些评论。由于各种原因,我还丢失了存储代码的存储库的历史记录:我正在尝试通过内存重建解决方案,所以我不会打赌它会确定答案 - 也许这只是对您进行精细搜索的提示可以让自己更接近比我更知情的消息来源。

然而,事情是这样的:我认为它在布局方面得到了解决。 IIRC 的诀窍是在地图上添加一个具有透明背景的视图。我已经在地图上有了一个叠加视图,所以我只需要更改背景颜色。生成的 XML 文件大致如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout 
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">

        <ImageView
            android:id="@+id/menu_button"
            android:layout_width="@dimen/menu_button_side"
            android:layout_height="@dimen/menu_button_side"
            android:src="@drawable/red_menu_button"
            android:scaleType="centerCrop" />

    </RelativeLayout>

</FrameLayout>

罪魁祸首是一些用于渲染地图的 Open GL 视图的背景,默认情况下它是黑色的,或者其他什么。我相信这确实是某处的 Android 错误报告。因此,请尝试在地图上覆盖具有透明背景的布局或视图,看看是否可行。在我的情况下,不祥的黑色封面消失了,但在菜单滑动动画期间,它在地图下仍然清晰可见。无论如何,我认为只是一个小故障,并且对结果很满意。我希望这会有所帮助。

【讨论】:

  • @Segi ...这是我建议的 XML 的 Java 等价物。很高兴能帮上忙。
【解决方案2】:

最后,我通过在 inflaterView 之后的 ListingStreetViewFragment.java 文件中添加以下代码解决了我的问题:

FrameLayout frameLayout = new FrameLayout(getActivity());
            frameLayout.setBackgroundColor(
                getResources().getColor(android.R.color.transparent));
            ((ViewGroup) inflatedView).addView(frameLayout,
                new ViewGroup.LayoutParams(
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.FILL_PARENT
                )
            );

【讨论】:

    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 2013-05-23
    相关资源
    最近更新 更多