2016-11-25 73 views
0

当Mapfragment位于ConstraintLayout中时,Android Studio 2.2中的Google Maps项目模板失败。只需将ConstraintLayout更改为“RelativeLayout”即可解决问题。ConstraintLayout中的Android MapFragment失败

以下在activity_main.xml中正常工作 - 即。谷歌地图出现在手机上。

如果“RelativeLayout的”改为“android.support.constraint.ConstraintLayout”然后我得到的日志中相同的信息,但我只是得到一个白色的屏幕 - 没有地图。

public class MainActivity extends Activity implements OnMapReadyCallback { 
    private GoogleMap mMap; 

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


/** 
* 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)); 
    } 
} 

的build.gradle ..

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.0" 
    defaultConfig { 
     applicationId "tech.trilobite.geology" 
     minSdkVersion 15 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:25.0.1' 
    compile 'com.google.android.gms:play-services:10.0.0' 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4' 
} 

..和不显示的谷歌地图(只是一个白色画面)ConstraintLayout ..

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/constraintLayout" 
    tools:context="tech.trilobite.geology.MainActivity"> 

    <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.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="tech.trilobite.geology.MainActivity" 
     /> 

</android.support.constraint.ConstraintLayout> 

..和'Android Monitor'日志(这与RelativeLayout和ConstraintLayout相同,但前者可行,后者不会)。

11-26 09:14:24.569 30654-30654/? I/art: Late-enabling -Xcheck:jni 
11-26 09:14:24.599 30654-30661/? E/art: Failed sending reply to debugger: Broken pipe 
11-26 09:14:24.608 30654-30661/? I/art: Debugger is no longer active 
11-26 09:14:24.608 30654-30661/? I/art: Starting a blocking GC Instrumentation 
11-26 09:14:24.626 30654-30654/? W/System: ClassLoader referenced unknown path: /data/app/tech.trilobite.geology-1/lib/arm64 
11-26 09:14:24.639 30654-30654/? I/InstantRun: Instant Run Runtime started. Android package is tech.trilobite.geology, real application class is null. 
11-26 09:14:26.222 30654-30654/tech.trilobite.geology W/System: ClassLoader referenced unknown path: /data/app/tech.trilobite.geology-1/lib/arm64 
11-26 09:14:26.267 30654-30654/tech.trilobite.geology I/FirebaseInitProvider: FirebaseApp initialization unsuccessful 
11-26 09:14:26.432 30654-30654/tech.trilobite.geology I/zzai: Making Creator dynamically 
11-26 09:14:26.558 30654-30654/tech.trilobite.geology W/System: ClassLoader referenced unknown path: 
11-26 09:14:26.651 30654-30654/tech.trilobite.geology W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000018/n/arm64-v8a 
11-26 09:14:26.706 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.721 30654-30654/tech.trilobite.geology I/Google Maps Android API: Google Play services client version: 10084000 
11-26 09:14:26.735 30654-30654/tech.trilobite.geology I/Google Maps Android API: Google Play services package version: 10084440 
11-26 09:14:26.758 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.784 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.799 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.811 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.825 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.844 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.855 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.887 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.887 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 
11-26 09:14:26.889 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.935 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:26.937 30654-30654/tech.trilobite.geology I/c: Token loaded from file. Expires in: 344773986 ms. 
11-26 09:14:26.937 30654-30654/tech.trilobite.geology I/c: Scheduling next attempt in 344473 seconds. 
11-26 09:14:27.014 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 
11-26 09:14:27.014 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 
11-26 09:14:27.037 30654-30706/tech.trilobite.geology D/NetworkSecurityConfig: No Network Security Config specified, using platform default 
11-26 09:14:27.130 30654-30732/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:27.130 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:27.131 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 
11-26 09:14:27.302 30654-30735/tech.trilobite.geology I/Adreno: QUALCOMM build     : 74df444, I409c65498b 
                   Build Date      : 06/22/16 
                   OpenGL ES Shader Compiler Version: XE031.08.00.02 
                   Local Branch      : N16 
                   Remote Branch     : 
                   Remote Branch     : 
                   Reconstruct Branch    : 
11-26 09:14:27.315 30654-30735/tech.trilobite.geology I/OpenGLRenderer: Initialized EGL, version 1.4 
11-26 09:14:27.315 30654-30735/tech.trilobite.geology D/OpenGLRenderer: Swap behavior 1 
11-26 09:14:27.599 30654-30750/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:27.895 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
11-26 09:14:27.897 30654-30654/tech.trilobite.geology W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 
11-26 09:14:29.151 30654-30733/tech.trilobite.geology W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found. 
11-26 09:14:29.160 30654-30733/tech.trilobite.geology I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:2 
11-26 09:14:29.160 30654-30733/tech.trilobite.geology I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 2 
+0

你应该包括在你的问题不起作用的布局。 –

回答

1

这看起来像一个ConstraintLayout问题。你没有使用任何限制!

ConstraintLayout使用约束让视图相对于彼此定位,也可以用它们来定义视图的尺寸。

对于ConstraintLayout中的视图,正确的等效于match_parent的目的是将高度和/或宽度设置为0dp(有时称为“匹配约束”),然后确保视图具有对其设置的适当约束。你的情况:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/constraintLayout" 
    tools:context="tech.trilobite.geology.MainActivity"> 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     tools:context="tech.trilobite.geology.MainActivity" 
     /> 

</android.support.constraint.ConstraintLayout> 

这里的片段的高度和宽度都设置为0dp,这意味着它看起来它的约束,以确定其大小。这些约束将片段的所有四边连接到其父项的边缘,以便它将扩展以填充该空间。

之前可能发生的情况是该片段正在工作,但您无法看到它,因为它的大小为零。

+1

辉煌!谢谢,刘易斯。这一切都很好 - Google地图现在出现在屏幕上。 – Tybion

+0

非常好!如果解决问题↑Please,请将答案标记为已接受 –

0

它看起来像一个gradle这个问题的优化,你应该尝试禁用即时运行,并禁用此:

buildTypes { 
    release { 
     minifyEnabled false 
     shrinkResources false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
    debug { 
     minifyEnabled false 
     shrinkResources false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
+0

感谢您的帮助。不过,我是全新的Android开发。 我应该将这部分build.gradle文件添加到?我试图把它放在DefaultConfig下,但是系统出现了一个错误。 – Tybion

+0

我更新了答案,让我知道它是否有效 – Jaythaking

+0

谢谢。更改build.gradle,重建和运行,但相同的日志消息和相同的结果 - 白屏。 – Tybion

相关问题