我最近在我的应用程序中包含了ACRA,其中包括一个GoogleMapSupportFragment.我现在得到一些崩溃报告,我无法在我的设备上重现.如果我想在地图上放置新的标记,应用程序会崩溃.每个标记都与一个位置连接,该位置还有其他信息(地址,名称,……).
这是我管理GoogleMap的课程
public SignManager(MainActivity activity, GoogleMap map) {
mActivity = activity;
this.mMap = map;
initMap();
}
private void initMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
//...
}
这是函数,它(有时)抛出NullPointerException.从AsyncTasks的LocalBroadcast调用函数updateSignsInView,它从服务器接收带有新标记的更新.
public void updateSignsInView(LocationWO[] mSignArray) {
if(mSignArray == null){
Log.e(DEBUG_TAG,"updateSignsInView() -> mSignArray null");
return;
}
resetMap(); //delete some unneeded data and do mMap.clear()
mMarkerLocationMap = new HashMap<Marker, LocationWO>();
for (int i = 0; i < mSignArray.length; i++) {
//!!! this is the bad line: SignManager.java:101
Marker marker = mMap.addMarker(createMapMarker(mSignArray[i]));
mMarkerLocationMap.put(marker, mSignArray[i]);
}
}
//create a new Marker with given information
private MarkerOptions createMapMarker(LocationWO mLocation) {
MarkerOptions mMarker = new MarkerOptions()
.position(
new LatLng(mLocation.getmLatitude(), mLocation
.getmLongitude()))
.title(mLocation.getmName()
.icon(BitmapDescriptorFactory.fromBitmap(icon));
return mMarker;
}
这是我收到的LogCat:
USER_COMMENT=null
ANDROID_VERSION=4.1.1
APP_VERSION_NAME=0.9
BRAND=htc_europe
PHONE_MODEL=HTC One X
CUSTOM_DATA=
STACK_TRACE=java.lang.NullPointerException
at maps.aj.bk.a(Unknown Source)
at maps.aj.bk.a(Unknown Source)
at maps.aj.al.a(Unknown Source)
at bcn.onTransact(SourceFile:167)
at android.os.Binder.transact(Binder.java:326)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
at MYAPP.SignManager.updateSignsInView(SignManager.java:101)
at MYAPP.MainActivity$1.onReceive(MainActivity.java:389)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
at dalvik.system.NativeStart.main(Native Method)
我99.99%肯定,mMap不能为空.在onCreate()方法中设置mMap.
任何想法?
编辑:setupMap功能
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map_screen)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
解决方法:
这里的问题是传递的图标为null.
But if the Bitmap-Icon is null, shouldn’t it crash in the createMarker()method? Or will it crash later, because there is no icon tp place at the map?
是的,如果它直接将NPE抛入GoogleMap.addMarker或BitmapDescriptorFactory.fromBitmap中会更好,但除非您在图标上调用方法,否则它不会在您的createMapMarker中发生.
由于问题中没有相关代码,因此无法判断它为何为空,但我怀疑当用户离开应用程序时会终止该进程,并在以后重新创建它们.