2017-07-05 217 views
0

所以我試圖做一個簡單的應用程序與谷歌地圖片段。我從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.該應用程序將運行,但不會顯示地圖片段。

+0

'android:layout_height =「1720dp」'看起來很奇怪 –

+0

這是因爲地圖不應該是整個屏幕。應該有一個工具欄的地方。 – MrHarrison

+0

然後使用LinearLayout ...沒有理由對代碼的大小進行硬編碼 –

回答

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" 

這將只需將您的工具欄上方和下方的部分作爲地圖片段。

現在,不顯示的地圖:

你已經把谷歌發揮你的應用程序服務依賴?

還將此添加到您的清單文件正上方的權限。

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

對不起,我不能發表評論,所以張貼解答。

希望它有幫助!