“IBitmapDescriptorFactory未初始化”错误
我正在试图在Google Maps Android API v2中使用自定义图标。 我只是改变了Google提供的一个例子。 我将.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow))
到方法setUpMap().
RawMapViewDemoActivity中setUpMap().
private void setUpMapIfNeeded() { if (mMap == null) { mMap = ((MapView) findViewById(R.id.map)).getMap(); if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMarker(new MarkerOptions() .position(new LatLng(0, 0)) .title("Marker") .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow))); }
但我总是得到一个“IBitmapDescriptorFactory未初始化”。
12-18 15:40:54.356: E/AndroidRuntime(12591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapdemo/com.example.mapdemo.RawMapViewDemoActivity}: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.access$600(ActivityThread.java:130) 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.os.Handler.dispatchMessage(Handler.java:99) 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.os.Looper.loop(Looper.java:137) 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.main(ActivityThread.java:4745) 12-18 15:40:54.356: E/AndroidRuntime(12591): at java.lang.reflect.Method.invokeNative(Native Method) 12-18 15:40:54.356: E/AndroidRuntime(12591): at java.lang.reflect.Method.invoke(Method.java:511) 12-18 15:40:54.356: E/AndroidRuntime(12591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 12-18 15:40:54.356: E/AndroidRuntime(12591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 12-18 15:40:54.356: E/AndroidRuntime(12591): at dalvik.system.NativeStart.main(Native Method) 12-18 15:40:54.356: E/AndroidRuntime(12591): Caused by: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized 12-18 15:40:54.356: E/AndroidRuntime(12591): at com.google.android.gms.internal.at.a(Unknown Source) 12-18 15:40:54.356: E/AndroidRuntime(12591): at com.google.android.gms.maps.model.BitmapDescriptorFactory.d(Unknown Source) 12-18 15:40:54.356: E/AndroidRuntime(12591): at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromResource(Unknown Source) 12-18 15:40:54.356: E/AndroidRuntime(12591): at com.example.mapdemo.RawMapViewDemoActivity.setUpMap(RawMapViewDemoActivity.java:67) 12-18 15:40:54.356: E/AndroidRuntime(12591): at com.example.mapdemo.RawMapViewDemoActivity.setUpMapIfNeeded(RawMapViewDemoActivity.java:58) 12-18 15:40:54.356: E/AndroidRuntime(12591): at com.example.mapdemo.RawMapViewDemoActivity.onCreate(RawMapViewDemoActivity.java:43) 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.Activity.performCreate(Activity.java:5008) 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 12-18 15:40:54.356: E/AndroidRuntime(12591): ... 11 more
在BitmapDescriptorFactory中它说:
在使用这个类的任何方法之前,你必须执行以下操作之一来确保这个类被初始化:
等待GoogleMap可以从您添加到应用程序的MapFragment或MapView中获得。 您可以通过调用getMap()方法并检查返回的对象是否为空来validationGoogleMap是否可用。
调用MapsInitializer.initialize(Context)。 只要不引发com.google.android.gms.common.GooglePlayServicesNotAvailableException GooglePlayServicesNotAvailableException,该类将被正确初始化。
我做了第一个,但仍然得到这个错误。 有什么build议么?
在onCreate()
调用MapsInitializer.initialize(getApplicationContext())
onCreate()
在onCreate()
调用以下内容
try { MapsInitializer.initialize(getApplicationContext()); } catch (GooglePlayServicesNotAvailableException e) { // TODO Auto-generated catch block e.printStackTrace(); }
将您的代码移动到名为“onMapReady()”的位置,这是由GoogleMaps API提供的callback。
这将工作!
这发生在我身上,最后在我的情况下,我能find的是谷歌播放服务没有安装。 所以安装它们,应用程序没有崩溃。 所以可能是一个可以把尝试和对话这样在catch块。
GoogleApiAvailability googleApiAvailability=GoogleApiAvailability.getInstance(); int status=googleApiAvailability.isGooglePlayServicesAvailable(getActivity()); if (status != ConnectionResult.SUCCESS) { int requestCode = 10; Dialog dialog = googleApiAvailability.getErrorDialog(getActivity(),status,requestCode); dialog.show(); }else {}