为什么没有显示地图片段?

时间:2017-07-05 21:30:48

标签: java android google-maps android-fragments

因此,我尝试使用Google Maps Fragment进行简单的应用程序。我从Android-Studio的预设开始。从新开始,显示工具栏但不显示地图片段。接下来,您将看到我的代码和堆栈跟踪:

activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="1080dp"
    android:layout_height="1920dp"
    tools:context=".MapsActivity">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="1720dp"
        tools:context="com.example.testingmapingmarker23.MapsActivity"
        />



    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"

        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</RelativeLayout>

MapsActivity.java

package com.example.testingmapingmarker23;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;



import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;



import static com.example.testingmapingmarker23.R.id.map;

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(map);
        mapFragment.getMapAsync(this);

        final Toolbar customToolbar = (Toolbar) findViewById(R.id.toolbar);



    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    }





}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testingmapingmarker23">

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">



        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value=" AIz...." />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

堆栈跟踪

/com.example.testingmapingmarker23 W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000023/n/armeabi-v7a
07-05 23:22:08.294 21301-21845/com.example.testingmapingmarker23 W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000023/n/armeabi
07-05 23:22:08.296 21301-21849/com.example.testingmapingmarker23 W/Adreno-ES20: <core_glEGLImageTargetTexture2DOES:4548>: GL_INVALID_VALUE
07-05 23:22:08.298 21301-21849/com.example.testingmapingmarker23 W/Adreno-ES20: <core_glEGLImageTargetTexture2DOES:4548>: GL_INVALID_VALUE
07-05 23:22:08.298 21301-21849/com.example.testingmapingmarker23 E/GLConsumer: [SurfaceTexture-0-21301-0] bindTextureImage: error binding external image: 0x501
07-05 23:22:08.343 21301-21849/com.example.testingmapingmarker23 W/Adreno-ES20: <core_glEGLImageTargetTexture2DOES:4548>: GL_INVALID_VALUE
07-05 23:22:08.345 21301-21849/com.example.testingmapingmarker23 W/Adreno-ES20: <core_glEGLImageTargetTexture2DOES:4548>: GL_INVALID_VALUE

这一遍又一遍地重复。我使用的是带有Android 6.0.1的OnePlus One并显示了API 23.应用程序运行,但它没有显示地图片段。

1 个答案:

答案 0 :(得分:0)

布局:

如果您使用的是自定义工具栏,请更好地将 样式 更改为 的 NoActionBar

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

使用相对布局,  对于工具栏,请保持

android:layout_alignParentTop="true"

和该地图片段

 android:layout_below="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="match_parent"

这只会将工具栏放在顶部和下部作为地图片段。

现在地图没有显示:

您是否已将 Google Play服务依赖项添加到您的应用中?

同时将此文件添加到您的权限之上的清单文件中。

 <permission
        android:name="your_package_name.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

抱歉,我无法发表评论,所以发布了一个答案。

希望它有所帮助!!