使用Google Map API v2的ViewPager变黑

时间:2014-01-17 07:29:07

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

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

截图:

enter image description here

代码:

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中没有显示错误。所以,我无法从那里追踪它。

2 个答案:

答案 0 :(得分: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错误报告。因此,请尝试在地图上叠加带有透明背景的布局或视图,看看是否有效。在我的情况下,不祥的黑色封面消失了,但在菜单的滑动动画期间,它仍然在地图下清晰可见。无论如何,我只考虑了一个小故障,并对结果感到满意。我希望这会有所帮助。

答案 1 :(得分:1)

最后,我在 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
                )
            );