地图片段在简历上崩溃

时间:2017-03-12 16:40:40

标签: java android nullpointerexception android-mapview android-maps-v2

我试图在标签中显示地图视图片段但由于某种原因,每当我轻拂切换开关并转到其他标签时,每当我返回包含地图的标签时,应用程序都会崩溃。我查看了onResume()代码,但我不确定课程中需要更改的内容。

public class FragmentCentralParkLocation extends android.support.v4.app.Fragment implements OnMapReadyCallback {

    public FragmentCentralParkLocation() {
        // Required empty constructor
    }

    GoogleMap mGoogleMap;
    MapView mMapView;

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

        mMapView = (MapView) v.findViewById(R.id.map_centralpark);
        mMapView.onCreate(savedInstanceState);
        mMapView.getMapAsync(this); //this is important

        SwitchCompat swt = (SwitchCompat) v.findViewById(R.id.switch_map_centralpark);
        swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    boolean success = mGoogleMap.setMapStyle(new MapStyleOptions(getResources()
                            .getString(R.string.style_json)));    
                }else{
                    boolean success = mGoogleMap.setMapStyle(null);
                }
            }
        });



        return v;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
        mGoogleMap.setBuildingsEnabled(true);
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        // Add markers and move the camera
        LatLng knt_location = new LatLng(40.782893, -73.965422);
        mGoogleMap.addMarker(new MarkerOptions()
                .position(knt_location)
        );


        // Updates the location and zoom level of the MapView
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(40.782893, -73.965422), 18);
        mGoogleMap.animateCamera(cameraUpdate);
    }

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

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

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

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

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

第1阶段

enter image description here

第2阶段

enter image description here

第3阶段

enter image description here

logcat的

03-12 16:45:24.965 2328-2328/com.ustravel.parks E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.ustravel.parks, PID: 2328
                                                                                    java.lang.NullPointerException
                                                                                        at com.ustravel.parks.FragmentCentralParkLocation $1.onCheckedChanged(FragmentCentralParkLocation.java:49)
                                                                                        at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
                                                                                        at android.support.v7.widget.SwitchCompat.setChecked(SwitchCompat.java:1062)
                                                                                        at android.widget.CompoundButton.onRestoreInstanceState(CompoundButton.java:381)
                                                                                        at android.view.View.dispatchRestoreInstanceState(View.java:12799)
                                                                                        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2643)
                                                                                        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2643)
                                                                                        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2643)
                                                                                        at android.view.View.restoreHierarchyState(View.java:12777)
                                                                                        at android.support.v4.app.Fragment.restoreViewState(Fragment.java:475)
                                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1329)
                                                                                        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
                                                                                        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
                                                                                        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                                                                                        at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
                                                                                        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1979)
                                                                                        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
                                                                                        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
                                                                                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
                                                                                        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668)
                                                                                        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
                                                                                        at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)

onResume地图结果

enter image description here

1 个答案:

答案 0 :(得分:2)

我认为您在SwitchCompat准备好并初始化之前尝试恢复GoogleMap的状态。在这里,我稍微修改了你的代码:

public class FragmentCentralParkLocation extends android.support.v4.app.Fragment implements OnMapReadyCallback {

    public FragmentCentralParkLocation() {
        // Required empty constructor
    }

    GoogleMap mGoogleMap;
    MapView mMapView;
    SwitchCompat swt;

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

        mMapView = (MapView) v.findViewById(R.id.map_centralpark);
        mMapView.onCreate(savedInstanceState);
        mMapView.getMapAsync(this); //this is important

        swt = (SwitchCompat) v.findViewById(R.id.switch_map_centralpark);

        return v;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
        mGoogleMap.setBuildingsEnabled(true);
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               initMap(isChecked);
            }
        });

        initMap(swt.isChecked());

        // Add markers and move the camera
        LatLng knt_location = new LatLng(40.782893, -73.965422);
        mGoogleMap.addMarker(new MarkerOptions()
                .position(knt_location)
        );


        // Updates the location and zoom level of the MapView
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(40.782893, -73.965422), 18);
        mGoogleMap.animateCamera(cameraUpdate);
    }

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

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

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

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

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

    private void initMap(boolean isChecked){
          if(isChecked){
                    mGoogleMap.setMapStyle(new MapStyleOptions(getResources()
                            .getString(R.string.style_json)));    
                }else{
                    mGoogleMap.setMapStyle(null);
                }
    }
}

我希望这会对你有所帮助。

修改:如果您仍然遇到崩溃,可以尝试通过以下方式尝试删除onCheckedChangeListenerSwitchCompat中的onStop onDestroy代码:

swt.setonCheckedChangeListener(null);