我有Android应用程序,我在我的设备上开发了几个月,可以在API 19上运行。现在,当我使用API 22获得新手机时,所有工作除了谷歌地图。这个API上的谷歌地图有什么特别之处,或者我做错了什么?在应用程序设置中我有
minSdkVersion 15
targetSdkVersion 23
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.google.android.gms:play-services:7.8.0'
//tried this but still wont work
//compile 'com.google.android.gms:play-services:8.4.0'
}
地图活动的代码是,我有评论//发生错误
public class MapLocation extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
public Double lat;
public Double log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_location);
setUpMapIfNeeded();
Intent i = getIntent();
lat = Double.parseDouble(i.getStringExtra("lat"));
log = Double.parseDouble(i.getStringExtra("log"));
centerMapOnMyLocation();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
private void centerMapOnMyLocation() {
//HERE is ERROR when checking log of phone
//HERE is ERROR when checking log of phone
//HERE is ERROR when checking log of phone
//HERE is ERROR when checking log of phone
mMap.setMyLocationEnabled(true);
if (!(lat == 0.0 || log == 0.0)) {
CameraUpdate zoom=CameraUpdateFactory.zoomTo(18);
CameraUpdate myLocation= CameraUpdateFactory.newLatLng(new LatLng(lat,log));
mMap.moveCamera(myLocation);
mMap.animateCamera(zoom);
mMap.addMarker(new MarkerOptions().position(new LatLng(lat, log)).title("Title"));
Toast.makeText(this, "Map positioned", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "no location", Toast.LENGTH_LONG).show();
}
}
}
手机记录
java.lang.RuntimeException:无法启动活动ComponentInfo {com.smth / com.smth.MapLocation}:java.lang.NullPointerException:尝试调用虚拟方法'void com.google.android.gms.maps.GoogleMap空集对象引用上的.setMyLocationEnabled(boolean)' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5255) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean)' at com.smth.MapLocation.centerMapOnMyLocation(MapLocation.java:83) 在com.vodovod.ivan.watercounter.MapLocation.onCreate(MapLocation.java:30) 在android.app.Activity.performCreate(Activity.java:6036) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5255) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
地图布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:keepScreenOn="true">
<fragment 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" android:id="@+id/map"
tools:context="com.vodovod.ivan.watercounter.MapLocation"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:keepScreenOn="true" />
</LinearLayout>
答案 0 :(得分:1)
Map无法立即供您使用,这就是为什么它表示您正在调用null对象。实现OnMapReadyCallback接口,从该接口覆盖onMapReady方法,并在该方法中调用涉及Google Map实例的方法。
答案 1 :(得分:0)
如@Fustigador所述,您会收到NullPointerException,因为尚未初始化地图对象。要解决您的问题,请将getMap()
替换为
getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
centerMapOnMyLocation(map);
}
});