しばらく取り組んできたアプリがあります。メインアクティビティは、Android(v2)用のGoogleMapクラスを使用した地図を表示します。常にではありませんが、アプリの起動時にnullポインターが表示されることがあります。前回これが発生したときは、コードに変更を加えることなく、1日後に発生しなくなりました。logcat(以下に投稿)を見た後、マップオブジェクトがまだ利用できないという予感がしました。
そこで、https://developers.google.com/maps/documentation/android/map([マップの可用性の確認]セクション)のページを確認しました。onCreateでマップオブジェクトがnullではないと既に判断しています。
この問題は断続的に発生するため、Googleサービスと関係があると推測できます。最近、Googleサービスフレームワークアプリのデータを強制的に停止してクリアし、4.2.2へのアップデートを取得しようとしました(訴えます)。何か考え-誰かがこれを聞いたことがありますか、または誰かがそれを回避する方法を知っていますか?
編集:このコードは、ここに表示されているとおりに再び機能するようになりました。何も変更していません。
だから私の質問は:この種の行動を引き起こす可能性があるのは何ですか?GoogleのMapsforAndroid v2ドキュメントのコードを使用して、onCreate()、onStart()、onResume()でマップオブジェクト(以下の142行目のNPE)が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
主な活動:
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);