Android Studio集成Google Maps模拟器崩溃问题求助
排查Google Maps Android项目崩溃问题及可运行示例
一、崩溃核心排查步骤
- 优先抓取崩溃日志:打开Android Studio的
Logcat面板,过滤Error级别,定位崩溃堆栈信息(常见诱因:API密钥配置错误、权限缺失、依赖版本不兼容、布局组件配置错误) - 验证API密钥有效性:
- 确认
AndroidManifest.xml中密钥配置格式正确:<meta-data android:name="com.google.android.geo.API_KEY" android:value="你的API密钥"/> - 检查Google Cloud控制台:该密钥需启用
Maps SDK for Android服务,且已配置对应应用的SHA-1签名和包名
- 确认
- 核对权限配置:确保
AndroidManifest.xml添加必要权限:<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-feature android:name="android.hardware.location.gps" android:required="false"/> - 检查Gradle依赖兼容性:确保
build.gradle(Module level)中Maps依赖版本与Android Gradle Plugin适配,示例:dependencies { implementation 'com.google.android.gms:play-services-maps:18.1.0' implementation 'com.google.android.gms:play-services-location:21.0.1' } - 验证布局组件:
main_activity.xml中SupportMapFragment配置需正确,避免组件类名拼写错误
二、可运行的基础Google Maps项目示例
1. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mapdemo"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MapDemo"> <meta-data android:name="com.google.android.geo.API_KEY" android:value="你的API密钥"/> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <fragment android:id="@+id/map_fragment" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
3. MainActivity.java
package com.example.mapdemo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map_fragment); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // 可在此添加标记、移动相机等操作,示例: // LatLng sydney = new LatLng(-34, 151); // mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); // mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } }
4. Module-level build.gradle(关键片段)
plugins { id 'com.android.application' } android { namespace 'com.example.mapdemo' compileSdk 34 defaultConfig { applicationId "com.example.mapdemo" minSdk 21 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.gms:play-services-maps:18.1.0' implementation 'com.google.android.gms:play-services-location:21.0.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
内容的提问来源于stack exchange,提问作者Raye Nelson
相关产品推荐
相关产品推荐

