NullPointerException:未初始化CameraUpdateFactory

时间:2014-12-15 09:04:37

标签: java android map fragment

当应用程序尝试启动onLocationChanged时,我在logcat中收到错误,声明相机更新未初始化,是否有某些内容丢失或出错?

所有这些代码在尝试单独的活动时都有效,但是我需要在3个标签的片段中使用它。

我感谢您的帮助和建议,即使这是一个愚蠢的问题

MAP FRAGMENT.CLASS

public class Map extends Fragment implements LocationListener{

private MapView mMapView;
private GoogleMap googleMap;
private Bundle mBundle;
LocationManager locationManager;
String provider;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View inflatedView = inflater.inflate(R.layout.map_layout, container, false);

    try {
        MapsInitializer.initialize(getActivity());
    } catch (Exception e) {
        e.printStackTrace();
    }

    mMapView = (MapView) inflatedView.findViewById(R.id.map);
    mMapView.onCreate(mBundle);
    setUpMapIfNeeded(inflatedView);

    return inflatedView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBundle = savedInstanceState;

    // LOCATION
    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    boolean enabledGPS = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean enabledWiFi = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    // Check if enabled and if not send user to the GSP settings
    if (!enabledGPS && !enabledWiFi) {
        Toast.makeText(getActivity(), "GPS signal not found", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    // Define the criteria how to select the location provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        Toast.makeText(getActivity(), "Selected Provider: " + provider,
                Toast.LENGTH_SHORT).show();
        onLocationChanged(location);
    }


}

private void setUpMapIfNeeded(View inflatedView) {
    if (googleMap == null) {
        googleMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
        if (googleMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    // Enable MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);

    //Enable MyLocationButton of Google Map
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

    // Set Map Type
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    //disable Pan Gesture
    googleMap.getUiSettings().setScrollGesturesEnabled(false);

}

@Override
public void onLocationChanged(Location location) {
   Log.e("onLocationChanged", "called");
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
    googleMap.animateCamera(cameraUpdate);
    // locationManager.removeUpdates(this);
}

public void onStatusChanged(String provider, int status, Bundle extras) { }

public void onProviderEnabled(String provider) {
    Toast.makeText(getActivity(), "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();
}

public void onProviderDisabled(String provider) {
    Toast.makeText(getActivity(), "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    mMapView.onDestroy();
    super.onDestroy();
}
}

logcat的

  java.lang.NullPointerException: CameraUpdateFactory is not initialized
        at com.google.android.gms.common.internal.o.b(Unknown Source)
        at com.google.android.gms.maps.CameraUpdateFactory.mp(Unknown Source)
        at com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(Unknown Source)
        at com.stephenh.daytrack.daytrackstephenh.SwipeViewsAdapter.Map.onLocationChanged(Map.java:122)
        at com.stephenh.daytrack.daytrackstephenh.SwipeViewsAdapter.Map.onCreate(Map.java:88)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:16628)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16628)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
        at android.view.View.measure(View.java:16628)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2295)
        at android.view.View.measure(View.java:16628)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1920)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1117)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1299)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5703)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:743)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5212)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

试试这个..

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
        new LatLng(yourLatitude, yourLongitude), zommvalue));