我在做一个 mapview 事例,但是它在 activity launch 上崩溃。我创建的目标是在version 2.1上Google API ,在模拟器和真机上都崩溃,也不能重写isRouteDisplayed 方法,贴出代码:
package com.sandy.hellogooglemaps;
import android.app.Activity;
import android.os.Bundle;
import com.google.android.maps.MapView;
public class HelloGoogleMaps extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
//@Override
protected boolean isRouteDisplayed() {
return false;
}
}
manifest file:
<application android:icon="@drawable/icon"
android:theme="@android:style/Theme.NoTitleBar"
android:label="@string/app_name">
<activity android:name=".HelloGoogleMaps"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
请求帮助!
ChatGPT尝试为您解答,仅供参考
看起来你的代码已经正确地设置了 MapView。但是,如果你的应用程序在启动时崩溃,这可能是由于地图 API 的问题导致的。
为了使地图 API 工作,你需要在 AndroidManifest.xml 文件中提供 Google Maps API 密钥。要获取密钥,请访问 Google Maps API 控制台,然后按照说明创建一个新的 API 密钥。然后,将该密钥添加到 AndroidManifest.xml 文件的 application 元素中,如下所示:
<application ...>
...
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR_API_KEY"/>
...
</application>
另外,请确保你已经在应用程序的 build.gradle 文件中包含了 Google Play 服务依赖项,如下所示:
dependencies {
implementation 'com.google.android.gms:play-services-maps:17.0.0'
}
如果你已经按照上述步骤进行操作,但仍然无法解决问题,你可以考虑从 Logcat 查看应用程序的崩溃日志,以了解崩溃的具体原因。