零地图组件在Android中初始化时的指针
我有一个应用程序,我一直在努力。主要活动显示使用Android版Google地图类(v2)的地图。有时,并不总是,当我启动我的应用时,我会得到一个空指针。上次发生这种情况时,一天后停止发生,代码没有任何变化。我预感到地图对象在查看logcat(下面发布)后仍然不可用。零地图组件在Android中初始化时的指针
所以我看了这个页面:https://developers.google.com/maps/documentation/android/map(验证地图可用性部分),并且我已经确定地图对象在onCreate中不为null。
由于此问题是间歇性的,我只能猜测它与Google服务有关。我最近强制停止并清除了Google服务框架应用程序的数据,以尝试更新到4.2.2(起诉我)。任何想法 - 有没有人听说过这个,还是有人知道如何解决它?
编辑:此代码现在再次工作,完全如您在这里看到的。我没有改变任何东西。
所以我的问题是:什么会导致这种行为?我正在使用直接来自Google Maps for Android v2文档的代码检查地图对象(下面第142行的NPE)是否在onCreate(),onStart()和onResume()中为null。
列入的onCreate(),在onStart()和的onResume()不应该允许这种...:
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mainMap))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
logcat的:
02-14 13:33:03.190: E/AndroidRuntime(5448): Caused by: java.lang.NullPointerException
02-14 13:33:03.190: E/AndroidRuntime(5448): at maps.ar.b.a(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448): at maps.y.h.a(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448): at maps.y.au.a(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448): at maps.y.ae.moveCamera(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448): at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:83)
02-14 13:33:03.190: E/AndroidRuntime(5448): at android.os.Binder.transact(Binder.java:310)
02-14 13:33:03.190: E/AndroidRuntime(5448): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448): at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448): at com.tyler.ioio.MainActivity.onStart(MainActivity.java:142)
02-14 13:33:03.190: E/AndroidRuntime(5448): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
02-14 13:33:03.190: E/AndroidRuntime(5448): at android.app.Activity.performStart(Activity.java:5114)
02-14 13:33:03.190: E/AndroidRuntime(5448): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
02-14 13:33:03.190: E/AndroidRuntime(5448): ... 11 more
MainActivity:
protected void onStart() {
super.onStart();
// This verification should be done during onStart() because the system
// calls this method when the user returns to the activity, which
// ensures the desired location provider is enabled each time the
// activity resumes from the stopped state.
mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final boolean gpsEnabled = mLocMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
new EnableGpsDialogFragment().show(getFragmentManager(),
"enableGpsDialog");
}
// Optimized code to go to location as fast as possible
Location firstLoc = mLocMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateNewFix(getBetterLocation(firstLoc, currentLoc));
// THIS IS LINE 142 : FORCE CLOSES
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, DEFAULT_ZOOM));
mMap.setOnMapClickListener(this);
我建议你像下面一样修改你的代码;
@Override
protected void onCreate(Bundle savedInstanceState) {
mMap.setOnMapClickListener(this);
}
protected void onStart() {
super.onStart();
// This verification should be done during onStart() because the system
// calls this method when the user returns to the activity, which
// ensures the desired location provider is enabled each time the
// activity resumes from the stopped state.
mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final boolean gpsEnabled = mLocMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
new EnableGpsDialogFragment().show(getFragmentManager(),
"enableGpsDialog");
}
@Override
protected void onResume() {
super.onResume();
setup();
private void setup() {
// Optimized code to go to location as fast as possible
Location firstLoc = mLocMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateNewFix(getBetterLocation(firstLoc, currentLoc));
// THIS IS LINE 142 : FORCE CLOSES
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, DEFAULT_ZOOM));
我认为在onStart()中添加toomany方法并不合适。
这是一个想法。我准确地瞄准了什么?只是清理onStart()一点点? – Tyler 2013-02-15 20:14:22
@Tyler ...请在制作Android应用程序前阅读[此功能](http://developer.android.com/training/basics/activity-lifecycle/starting.html),然后阅读[此程序](http: //developer.android.com/training/basics/location/locationmanager.html)在您的应用中实施Google地图。本文档是Android中的基本概念。 – BBonDoo 2013-02-16 00:00:59
好的,抱歉,我没有清楚地解释我在这里寻找什么。我写的代码有效。我现在正在使用它,没有更改onStart或onCreate。但是,在某些情况下,应用程序将强制关闭原始问题中显示的错误。发生这种情况时,会发生几个小时然后消失。到目前为止,它发生在我身上两次。我想我很难理解这将如何成为代码的一部分,而不是基础Google服务框架的一部分。新安装(应用程序以onCreate开头)和onResume都会发生这种情况。 – Tyler 2013-02-16 17:46:03
您是在设备上还是在仿真器上测试它?如果你有一个真正的Android设备,你可能会遇到我刚才遇到的同样的问题。我的手机实际上并没有关闭应用程序,所以当我以为我正在启动应用程序时,它真的会调用onResume()而不是onCreate(),这会导致未声明的对象等问题。这可能不是很接近,但它听起来可能是你的问题。 – 2013-02-14 20:37:48
你问题从这里开始:com.tyler.ioio.MainActivity.onStart(MainActivity.java LINE#142)你可以发布代码来显示你的MainActivity.java文件吗? – petey 2013-02-14 21:26:44
@BenBenard这是一款设备。我试图卸载应用程序之间的尝试无济于事。 – Tyler 2013-02-15 01:09:36