Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

System.NullReferenceException Xamarin.Forms.Platform.Android.Platform.OnLayout

$
0
0

Been getting this bug ever since I upgraded to 1.2.3.6275.

I have seen some threads saying this was a bug in 1.2.2 or 1.2.1. Seems to be an issue with my custom renderer.

10-17 02:29:07.335 E/mono-rt ( 1288): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
10-17 02:29:07.335 E/mono-rt ( 1288):   at Xamarin.Forms.Platform.Android.Platform.OnLayout (Boolean changed, Int32 l, Int32 t, Int32 r, Int32 b) [0x00000] in <filename unknown>:0 
10-17 02:29:07.335 E/mono-rt ( 1288):   at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (Boolean changed, Int32 l, Int32 t, Int32 r, Int32 b) [0x00000] in <filename unknown>:0 
10-17 02:29:07.335 E/mono-rt ( 1288):   at Android.Views.ViewGroup.n_OnLayout_ZIIII (IntPtr jnienv, IntPtr native__this, Boolean changed, Int32 l, Int32 t, Int32 r, Int32 b) [0x00000] in <filename unknown>:0 
10-17 02:29:07.335 E/mono-rt ( 1288):   at (wrapper dynamic-method) object:99e4ae3d-31ec-4c07-9bb3-fc4b39557d47 (intptr,intptr,bool,int,int,int,int)
10-17 02:29:07.335 W/        ( 1288): _wapi_connect: error looking up socket handle 0x31
public class MapContentPageRenderer: PageRenderer
    {
        Android.Views.View view;
        private MapFragment _mapFragment;

        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            var page = e.NewElement as MapContentPage;
            var activity = this.Context as Activity;

            view = activity.LayoutInflater.Inflate(Resource.Layout.MapLayout, this, false);
            AddView(view);
            InitMapFragment(activity);
            ZoomToPosition(page.InitLat, page.InitLng);
        }

        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);
            var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
            var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);
            view.Measure(msw, msh);
            view.Layout(0, 0, r - l, b - t);
        }

        private void InitMapFragment(Activity activity)
        {

            _mapFragment = activity.FragmentManager.FindFragmentByTag("map") as MapFragment;
            if (_mapFragment != null) return;
            var mapOptions = new GoogleMapOptions()
                .InvokeMapType(GoogleMap.MapTypeNormal)
                .InvokeRotateGesturesEnabled(false)
                .InvokeZoomControlsEnabled(false)
                .InvokeCompassEnabled(true);

            var fragTx = activity.FragmentManager.BeginTransaction();
            _mapFragment = MapFragment.NewInstance(mapOptions);

            fragTx.Add(Resource.Id.mapWithOverlay, _mapFragment, "map");
            fragTx.Commit();
        }

private void ZoomToPosition(double lat, double lng)
    {
        var latlng = new LatLng(lat, lng);
        var cameraPosition = new CameraPosition.Builder().Target(latlng).Zoom(14.0f).Build();
        var cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);
        _mapFragment.Map.MoveCamera(cameraUpdate);
    }
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>