谷歌地图V2安卓应用程序中的null数组错误

时间:2016-09-26 23:04:31

标签: android google-maps android-fragments android-maps-v2

我正在尝试将谷歌地图整合到一个片段中...应用程序运行良好之前但突然崩溃了

我检查了以下链接......但这些都不适合我 Attempt to get length of null array in google maps V2 android app

Why google map v2 suddenly crashed by java.lang.NullPointerException: Attempt to get length of null array?

我的代码是:

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MapsInitializer.initialize(getActivity());



        View bottomSheet = fragmentLayout.findViewById(R.id.bottomSheet);

        fap = (FloatingActionButton) fragmentLayout.findViewById(R.id.fap);
        fap.setOnClickListener(this);
        bottomSheetTitle = (TextView) fragmentLayout.findViewById(R.id.bottomSheetTitle);
        bottomSheetAddress = (TextView) fragmentLayout.findViewById(R.id.bottomSheetAddress);

        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);


        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    fap.setVisibility(View.GONE);
//                    text1.setText("Collapse Me!");
                } else {
                    fap.setVisibility(View.VISIBLE);
//                    text1.setText("Expand Me!");
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }


        });
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
//        getDetails(1);
//        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);


        switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
            case ConnectionResult.SUCCESS:
                Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
                mapView = (MapView) fragmentLayout.findViewById(R.id.map);
                if(mapView != null)
                    mapView.onCreate(savedInstanceState);
                // Gets to GoogleMap from the MapView and does initialization stuff
                if (mapView != null) {
                    Toast.makeText(getActivity(), "Getting map", Toast.LENGTH_SHORT).show();
                    mapView.getMapAsync(this);
                }
                break;
            case ConnectionResult.SERVICE_MISSING:
                Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
                break;
            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
                Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
                break;
            default:
//                Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
                break;
        }

//        googleMap.setMyLocationEnabled(true);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
//        Toast.makeText(getActivity(), "Map is ready to use", Toast.LENGTH_SHORT).show();
        this.map = googleMap;
        map.getUiSettings().setMyLocationButtonEnabled(false);
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            Toast.makeText(getActivity(), "Oops permission denied", Toast.LENGTH_SHORT).show();
            return;
        }
        map.setMyLocationEnabled(true);
        map.getUiSettings().setMyLocationButtonEnabled(true);
        mClusterManager = new ClusterManager<Location>(getActivity(), map);

        map.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
            @Override
            public void onCameraIdle() {
//                Toast.makeText(getActivity(), "camera moved", Toast.LENGTH_SHORT).show();
                getLocationByRadius(map.getCameraPosition().target.latitude, map.getCameraPosition().target.longitude);
            }
        });

        map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                getLocationByRadius(map.getCameraPosition().target.latitude, map.getCameraPosition().target.longitude);
            }
        });

        map.setOnCameraIdleListener(mClusterManager);
        map.setOnMarkerClickListener(mClusterManager);

        mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<Location>() {
            @Override
            public boolean onClusterItemClick(Location location) {
                getDetails(location.getLocationId());
                return false;
            }

        });


        checkPermission();


    }

xml layout:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="rollwithme.com.rollwithme.Fragments.ParkingFragment" app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <com.google.android.gms.maps.MapView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map" />

    <include layout="@layout/fragment_location_details" />

    <android.support.design.widget.FloatingActionButton
        android:src="@drawable/ic_expand_less_white_24dp"
        android:id="@+id/fap"
        app:fabSize="normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/bottomSheet"
        android:visibility="gone"
        app:backgroundTint="@color/colorPrimary"
        app:layout_anchorGravity="top|right|end"
        android:layout_marginRight="@dimen/cardViewPadding"/>

</android.support.design.widget.CoordinatorLayout>

我正在测试lg g3 .....应用程序在华为Y6 Pro中运行良好.....曾经在g3中工作.....

我得到的错误信息是

FATAL EXCEPTION: main
                                                                         Process: rollwithme.com.rollwithme, PID: 4401
                                                                         java.lang.NullPointerException: Attempt to get length of null array
                                                                             at maps.G.a.a(Unknown Source)
                                                                             at maps.V.N.a(Unknown Source)
                                                                             at maps.D.e.a(Unknown Source)
                                                                             at maps.D.p.a(Unknown Source)
                                                                             at maps.ad.ae.a(Unknown Source)
                                                                             at maps.ad.t.a(Unknown Source)
                                                                             at maps.ad.R.a(Unknown Source)
                                                                             at uo.onTransact(:com.google.android.gms.DynamiteModulesB:66)
                                                                             at android.os.Binder.transact(Binder.java:380)
                                                                             at com.google.android.gms.maps.internal.IMapViewDelegate$zza$zza.onCreate(Unknown Source)
                                                                             at com.google.android.gms.maps.MapView$zza.onCreate(Unknown Source)
                                                                             at com.google.android.gms.dynamic.zza$3.zzb(Unknown Source)
                                                                             at com.google.android.gms.dynamic.zza$1.zza(Unknown Source)
                                                                             at com.google.android.gms.maps.MapView$zzb.zzbru(Unknown Source)
                                                                             at com.google.android.gms.maps.MapView$zzb.zza(Unknown Source)
                                                                             at com.google.android.gms.dynamic.zza.zza(Unknown Source)
                                                                             at com.google.android.gms.dynamic.zza.onCreate(Unknown Source)
                                                                             at com.google.android.gms.maps.MapView.onCreate(Unknown Source)
                                                                             at rollwithme.com.rollwithme.Fragments.ParkingFragment.onActivityCreated(ParkingFragment.java:163)
                                                                             at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2089)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1133)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                                                                             at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                                                                             at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
                                                                             at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                                                                             at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                             at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
                                                                             at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
                                                                             at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
                                                                             at android.view.View.measure(View.java:17496)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
                                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
                                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
                                                                             at android.view.View.measure(View.java:17496)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                                             at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:700)
                                                                             at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:767)
                                                                             at android.view.View.measure(View.java:17496)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                             at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                             at android.view.View.measure(View.java:17496)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
                                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
                                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
                                                                             at android.view.View.measure(View.java:17496)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                             at android.view.View.measure(View.java:17496)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
                                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
                                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
                                                                             at android.view.View.measure(View.java:17496)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                             at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2636)
                                                                            at android.view.View.measure(View.java:17496

1 个答案:

答案 0 :(得分:0)

我无法使用mapview解决它 后来我切换到supportMapFragment ....它解决了我的问题...感谢谷歌

对于其他人,我按照以下链接...谢谢:)

https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment

https://developers.google.com/maps/documentation/android-api/map