Android的动作条与谷歌地图首次加载片段时,将变成黑色首次、片段、加载、黑色

2023-09-12 02:52:02 作者:你的幸福由我赞助

我有活动,我做Draw​​erLayout,我在侧面菜单5项,每个菜单项加载低于$ C $的片段(C,你会看到只有3个片段,那是因为最后两个菜单项是假的,现在)。该活动设置我的自定义动作条。活动及其onCreate方法看起来是这样的:

I have Activity where I made DrawerLayout where I have 5 entries in side menu and each menu entry is loading its fragment (below in code you will see only 3 Fragments, that's because last two menu entries are dummy for now). On that Activity I set my custom ActionBar. Activity and its onCreate method look something like this:

public class ActivityOffline extends ActionBarActivity implements OnPreferencesFragmentListener {
    final String[] fragments = { array with fragments path };

    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;

    private Fragment mF1
    private Fragment mF2
    private Fragment mF3

    @SuppressLint("InlinedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_offline);

        mF1 = Fragment.instantiate(ActivityOffline.this, fragments[0]);
        mF2 = Fragment.instantiate(ActivityOffline.this, fragments[1]);
        mF3 = Fragment.instantiate(ActivityOffline.this, fragments[2]);

        LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.action_bar_custom, null);

        final ActionBar actionBar = getSupportActionBar();

        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(v, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL
                | Gravity.FILL_HORIZONTAL));

        // Get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);

        // Get ListView defined in activity_main.xml
        drawerListView = (ListView)findViewById(R.id.left_drawer);

        // App Icon
        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_listview_item, drawerListViewItems));

        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.content_frame, mF1);
        tx.commit();

        // Create ActionBarDrawerToggle
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);

        // Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        // Еnable and show "up" arrow
        actionBar.setDisplayHomeAsUpEnabled(true);

        // Јust styling option
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        drawerListView.setOnItemClickListener(new DrawerItemClickListener());
    }
    }

当我在菜单项之间切换时,我总是替换片段是这样的:

When I am switching between menu items, I am always replacing fragments like this:

private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View view, final int position, long id) {
            drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);

                    if (position < 3) {
                        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();

                        switch (position) {
                        case 0:
                            tx.replace(R.id.content_frame, mF1);
                            break;
                        case 1:
                            tx.replace(R.id.content_frame, mF2);
                            break;
                        case 2:
                            tx.replace(R.id.content_frame, mF3);
                            break;
                        default:
                            tx.replace(R.id.content_frame, mF1);
                            break;
                        }

                        tx.commit();
                    }
                }
            });

            drawerLayout.closeDrawer(drawerListView);
        }
    }

所以,我的片段里面有谷歌地图是MF2。它的实施和布局文件是如下:

So, my Fragment which has Google Map is mF2. It's implementation and layout file are below:

执行:

public class FragmentRouteView extends Fragment {
    static final LatLng MY_LOCATION = new LatLng(54.002858, 7.956872);

    private MapView mMapView;
    private GoogleMap mGoogleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_route_view, container, false);
        mMapView = (MapView)v.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();

        try {
            MapsInitializer.initialize(getActivity());
        }
        catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }

        mGoogleMap = mMapView.getMap();

        // Move the camera instantly to start point with a zoom of 15.
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MY_LOCATION, 15));

        // Zoom in, animating the camera.
        mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

版式文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black" >

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

如果你问为什么我有这样的景观在布局文件 - 没有。它解决了一些问题,对我来说,拥有无关的问题我现在有。

If you are asking why do I have this View in layout file - don't. It solves some problem for me and has nothing to do with issue I currently have.

总之。这个介绍后,这里有一个问题:

Anyhow. After this intro, here's the problem:

当我第一次开始我的活动,其显示这些片段,我浏览第一时间MF2(其中有谷歌地图),我看地图,但我的动作条是全黑。无标题,无副作用按钮,显示抽屉菜单。另外,我看不出+和 - 谷歌地图的迹象。

When I first time start my activity which displays these fragments and I navigate first time to mF2 (which has Google Map), I see map, but my ActionBar is totally black. No title, no side button which displays drawer menu. Plus, I don't see + and - signs on Google Map.

下面是preVIEW:

Here's preview:

不过,虽然操作栏完全是黑色的,如果我在$地方,菜单按钮应该是,菜单显示和菜单按钮和标题p $ PSS再次显示。

But, although action bar is totally black, if I press on place where menu button should be, menu displays and menu button and title are displayed again.

这样的:

和之后,我没有遇到这种问题不断,而活动还活着。

And afterwards, I don't encounter this problem ever while activity is alive.

一切都那么细象下面显示的:

Everything's then fine like displayed below:

当活动被破坏,我重新开始,我会遇到这样的问题又来了,当我浏览第一时间MF2与谷歌地图。

When activity is destroyed and I start it again, I encounter this problem again when I navigate first time to mF2 with Google Map.

这不会发生在具有安卓4.xx的设备这只是发生在与Android 2.xx的设备

This doesn't happen on devices which have Android 4.x.x. It only happens on devices with Android 2.x.x.

任何想法是什么引起的?

Any idea what's causing this?

要prevent任何无关的计数器问题:

To prevent any unrelated counter questions:

是的,我使用的支持,正确的方法库 是的,我已经设置谷歌地图API V2关键正确 是的,我使用调试键,当我开始应用在设备上从Eclipse中 为什么我在我的地图片段布局中使用RelativeLayout的不仅com.google.android.gms.maps.MapView? - 我需要的神奇景观 由于某些原因,如果尝试与com.google.android.gms.maps.MapView 这也没有什么区别,同样的事情发生。 Yes, I am using support libraries in right way Yes, I have set Google Maps API V2 key properly Yes, I am using debug key when I am starting app on device from Eclipse Why do I use RelativeLayout in my Maps fragment layout and not only com.google.android.gms.maps.MapView? - I needed that magic View for some reason, and if try with com.google.android.gms.maps.MapView it also makes no difference, same thing happens.

在预先感谢。

:有此问题时,走了一个例子:

我忘了补充一点:

如果我注释掉这些行从我的地图片段实施:

If I comment out these lines from my Maps Fragment implementation:

// Move the camera instantly to start point with a zoom of 15.
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MY_LOCATION, 15));

// Zoom in, animating the camera.
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

我看到谷歌地图为中心的(​​0,0)坐标和动作条的是正确显示。因此,它看起来像,当我做一些额外的地图(缩放,定位,绘图标志等),就会出现此问题。

I see Google Map centered on (0,0) coordinates and ActionBar IS DISPLAYED CORRECTLY. So, it looks like that this problem occurs when I am doing something extra with map (zooming, positioning, drawing markers, etc).

推荐答案

已经有一些已知的问题(的 [1] ,的 [2] ,的 [3] )上产生一个黑盒子,在UI项的地图API V2鉴于顶部渲染UI项。

There have been some known issues ([1], [2], [3]) with rendering UI items on top of the Maps API v2 view that produce a black box in the UI item.

下面是从最新更新GMaps实现-API的问题,问题#4659 ,截至2013年8月27日:

Here's the latest update from gmaps-api-issues issue #4659, as of Aug. 27th 2013:

我们只是把改变地图的基本视图到TextureView上果冻豆4.1+(API 16+)设备的更新。这应解决的几个问题,包括黑色矩形时的地图视图在屏幕上移动。

We just pushed an update that changes the base view of the map to a TextureView on Jelly Bean 4.1+ (API 16+) devices. This should resolve several issues, including black rectangles when a map view is moved on the screen.

TextureView中引入了冰淇淋三明治,但我们发现驱动程序问题和/或TextureView的越野车实现许多ICS的设备。渲染文物不能为这些设备解决,或任何pre-ICS设备。

TextureView was introduced in Ice Cream Sandwich, but we found many ICS devices with driver issues and/or buggy implementations of TextureView. Rendering artifacts cannot be resolved for those devices, or any pre-ICS devices.

要确保你的地图可以使用TextureView,认为需要进行硬件加速。这可以在应用程序,活动或窗位来完成。见Android的文档,了解有关信息:    http://developer.android.com/guide/topics/graphics/hardware -accel.html

To ensure your map can use TextureView, the view needs to be hardware accelerated. This can be done on the Application, Activity or Window level. See Android’s documentation for information on this: http://developer.android.com/guide/topics/graphics/hardware-accel.html

有关于这个问题的评论,你可能会想尝试一些报道的解决方法,但现在看来,在pre-ICS设备潜在的问题不能得到解决。

There are some reported workarounds in comments on that issue that you may want to try, but it appears that the underlying problem on pre-ICS devices can't be resolved.