0

잠시 동안 작업해온 앱이 있습니다. 기본 활동에는 Android 용 GoogleMap 클래스 (v2)를 사용하는지도가 표시됩니다. 때로는 항상 그런 것은 아니지만 앱을 시작할 때 널 포인터를 얻습니다. 이 일이 일어난 마지막 시간에 코드 변경없이 하루가 지나지 않았습니다. 나는지도 객체가 아직 logcat (아래에 게시 됨)을보고 나서 사용할 수 없다는 직감을 가졌습니다.Null 포인터지도 요소가 Android에서 초기화 될 때

그래서이 페이지를 보았습니다 : https://developers.google.com/maps/documentation/android/map (지도 확인 섹션 확인), 그리고 이미지도 개체가 onCreate에서 null이 아니라고 결정했습니다.

이 문제는 간헐적이므로 Google 서비스와 관련이 있다고 추측 할 수 있습니다. 최근에 Force Stopped를 중단하고 Google 서비스 프레임 워크 앱의 데이터를 4.2.2로 업데이트하려고 시도했습니다 (소송). 어떤 생각 - 누구도이 말을 듣지 못했거나 주위를 둘러 보는 방법을 아는 사람이 있습니까?

편집 :이 코드는 여기에서 보는 것과 똑같이 다시 작동합니다. 나는 아무것도 바꾸지 않았다.

내 질문은 : 이런 종류의 동작이 발생할 수 있습니까? onCreate(), onStart() 및 onResume()에서 null이되는지도 객체 (아래 142 행의 NPE)를 확인하고 있습니다. Android v2 설명서의 Google지도에서 직접 코드를 사용합니다.

이 허용 안되는에서 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. 

로그 캣 :

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); 
+0

기기 또는 에뮬레이터에서 테스트 중이십니까? 실제 안드로이드 장치가 있다면 잠시 전에 만난 동일한 문제가있을 수 있습니다. 내 전화는 실제로 응용 프로그램을 닫지 않을 것이므로 내가 응용 프로그램을 시작할 때 onResume()을 호출하고 onCreate()는 호출하지 않을 것이므로 선언되지 않은 객체 등에서 문제가 발생합니다. 이것은 가까이 있지 않을 수도 있지만 문제가 될 수도 있습니다. –

+1

문제는 여기에서 시작됩니다 : com.tyler.ioio.MainActivity.onStart (MainActivity.java LINE # 142) MainActivity.java 파일을 보여주는 코드를 게시 할 수 있습니까? – petey

+0

@BenBenard 장치에 있습니다. 나는 쓸모없는 애플 리케이션을 중간에서 제거하려고 시도했다. – Tyler

답변

1

다음과 같이 코드를 수정하는 것이 좋습니다.

@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 메서드를 추가하는 것이 적절하지 않다고 생각합니다.

+0

그게 좋은 생각입니다. 이 목표를 정확히 목표로 무엇을해야합니까? onStart() 조금만 정리하면 되나요? – Tyler

+1

@Tyler ... Android 앱을 만들기 전에 [이 기능] (http://developer.android.com/training/basics/activity-lifecycle/starting.html)을 읽은 다음 [이 절차] (http : //developer.android.com/training/basics/location/locationmanager.html) 앱에서 Google지도를 구현할 수 있습니다. 이 문서는 Android의 기본 개념입니다. – BBonDoo

+0

알았어, 미안 내가 여기서 찾고있는 것을 분명히 설명하지 못했다. 필자가 작성한 코드가 작동합니다. 지금은 onStart 또는 onCreate를 변경하지 않고 바로 사용하고 있습니다. 그러나 경우에 따라 앱이 원래 질문에 표시된 오류와 강제 종료됩니다. 이런 일이 발생하면 몇 시간 동안 일어난 다음 사라집니다. 지금까지 두 번 나에게 일어난 일입니다. 이 코드가 기본 Google 서비스 프레임 워크의 일부가 아닌 코드의 일부가 될지 이해하는 데 어려움을 겪고 있습니다. 이것은 새로 설치 (onCreate로 시작하는 앱)와 onResume으로 발생합니다. – Tyler

관련 문제