Google 地图不显示在片段中

如果我发布的问题与许多其他问题类似,但每个问题都有不同的谷歌地图实现,并且尝试所有方法都意味着完全破坏我的代码,我很抱歉,因为我是 Android 开发新手,所以我宁愿如果你能帮我找出我的地图出了什么问题,就不要冒险了。谢谢。

当我运行我的应用程序时,我得到的是: 在此输入图像描述

这是我的 Gradle.build(应用程序):

apply plugin: 'com.android.application'


android {

    compileSdkVersion 28

    buildToolsVersion "29.0.2"

    defaultConfig {

        applicationId "com.example.binfo"

        minSdkVersion 27

        targetSdkVersion 28

        versionCode 1

        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {

        release {

            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

        }

    }

}


dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    implementation 'com.google.android.material:material:1.0.0'

    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'androidx.navigation:navigation-fragment:2.0.0'

    implementation 'androidx.navigation:navigation-ui:2.0.0'

    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'

    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'androidx.test:runner:1.1.1'

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    implementation 'com.google.android.gms:play-services-maps:17.0.0'

//    implementation 'com.google.android.gms:play-services:12.0.1'


    implementation 'com.google.firebase:firebase-analytics:17.2.0'

    implementation 'com.google.firebase:firebase-firestore:21.1.1'

}



apply plugin: 'com.google.gms.google-services'


函数式编程
浏览 114回答 2
2回答

牛魔王的故事

根据您的代码,我没有发现任何错误,但根据您的屏幕截图,您的互联网连接似乎丢失了,所以您能否先提供正确的互联网连接并检查一下。

不负相思意

在您的片段中使用 MapView:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; tools:context=".HomeFragment">&nbsp; &nbsp; <com.google.android.gms.maps.MapView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/mapView"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_below="@+id/llSelectDetails" /></RelativeLayout>在您的片段中使用:&nbsp; public class HomeFragment extends Fragment implements OnMapReadyCallback {&nbsp; &nbsp; private GoogleMap mMap;&nbsp; &nbsp; private MapView mMapView;&nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Inflate the layout for this fragment&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View rootView = inflater.inflate(R.layout.fragment_home, container, false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Configure Mapview and sync to google map.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mMapView = rootView.findViewById(R.id.mapView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mMapView.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mMapView.getMapAsync(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mMapView.onResume(); // needed to get the map to display immediately&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//your code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return rootView;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onMapReady(GoogleMap googleMap) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.mMap = googleMap;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }它会工作得很好。另外,请检查您的地图密钥和互联网连接。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java