Error on Android when SetPage(new MyMasterDetailPage()) "DrawerLayout must be measured with..."

Hi,

I've got an issue when I use the SetPage() method in my Android MainActivity and try to set a MasterDetailPage. The error message I get is:

Java.Lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.

I try to implement something similair as this example. However when I try to implement it, I get this error. The strange thing is that this happens even when I have a completely empty MasterDetailPage (except from the required parts). I also tried copying the MasterDetailPage from the example but that didn't work either.

When I use SetPage() to another regular ContentPage, it works fine.

(I am using Xamarin 1.2.2)

The MasterDetailPage, MainPage (PCL)

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  x:Class="//namespace//.Views.MainPage"
  Title="aaaa">

  <MasterDetailPage.Detail>
    <ContentPage Title="Main">
      <ContentPage.Content>
        <Label Text="bbbb" />
      </ContentPage.Content>
    </ContentPage>
  </MasterDetailPage.Detail>

  <MasterDetailPage.Master>
    <ContentPage Title="Menu">
      <ContentPage.Content>
        <Label Text="aaaa" />
      </ContentPage.Content>
    </ContentPage>
  </MasterDetailPage.Master>
</MasterDetailPage>

MainActivity.cs (Android Project)

[Activity(Label = "App Name", MainLauncher = true, Theme = "@android:style/Theme.Holo.Light", ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]

    public class MainActivity : AndroidActivity, IRootPageManager
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Xamarin.Forms.Forms.Init(this, bundle);

            SetPage(App.GetSplashPage(this));
        }

        public void ShowMainPage()
        {
            SetPage(App.GetMainPage(this));
        }

        public void ShowLoginPage()
        {
            SetPage(App.GetLoginPage(this));
        }
    }

Stacktrace

0x63 in Android.Runtime.JNIEnv.CallVoidMethod at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:507,138 C# 0x62 in Android.Views.View.Measure at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.View.cs:12243,1223 C# 0x73 in Xamarin.Forms.Platform.Android.VisualElementTracker.UpdateLayout C# 0xE in Xamarin.Forms.Platform.Android.MasterDetailRenderer.UpdateLayout C# 0x38 in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.NavigationPage>.OnLayout C# 0x8 in Xamarin.Forms.Platform.Android.NavigationRenderer.OnLayout C# 0x12 in Android.Views.ViewGroup.n_OnLayout_ZIIII at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.ViewGroup.cs:3274,1226 C# 0x2F in object.0b8fcd79-dec6-4fa9-9d9b-be0210db49d3 C# 0x52 in Android.Runtime.JNIEnv.CallVoidMethod at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:503,4 C# 0x87 in Android.Views.ViewGroup.Layout at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.ViewGroup.cs:3046,1226 C# 0x85 in Xamarin.Forms.Platform.Android.VisualElementTracker.UpdateLayout C# 0xE in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.NavigationPage>.UpdateLayout C# 0xA0 in Xamarin.Forms.Platform.Android.Platform.OnLayout C# 0x1B in Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout C# 0x12 in Android.Views.ViewGroup.n_OnLayout_ZIIII at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.ViewGroup.cs:3274,1226 C# 0x2F in object.0b8fcd79-dec6-4fa9-9d9b-be0210db49d3 C#

Posts

  • deckertron9000deckertron9000 USUniversity ✭✭✭

    @MatsHofman‌ From what I've seen I've only been able to use SetPage with a MasterDetailPage if I already have a NavigationPage onscreen. In my case I need to display the MasterDetailPage after logging on (My credentials page is wrapped in a NavigationPage) and that works. But when I try to use SetPage to set a different MasterDetailPage with a MasterDetailPage already onscreen (not wrapped in a NavigationPage), I get this error as well. Hope this helps a bit.

  • BobBirdBobBird USMember

    I have also been seeing the same error, but managed to find a work around.

    I found the following issue on stackoverflow: http://stackoverflow.com/questions/17687173/in-robolectric-how-do-i-get-around-drawerlayout-must-be-measured-with-measuresp

    To adapt this to Xamarin.Forms I just created a custom renderer like the following:

    [assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.MasterDetailPage), typeof(MyMasterDetailRenderer))]
    
    namespace Wireframes.Droid.Renderers
    {
        public class MyMasterDetailRenderer : MasterDetailRenderer
        {
            protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
            {
                widthMeasureSpec = MeasureSpec.MakeMeasureSpec(
                        MeasureSpec.GetSize(widthMeasureSpec), MeasureSpecMode.Exactly);
    
                heightMeasureSpec = MeasureSpec.MakeMeasureSpec(
                        MeasureSpec.GetSize(heightMeasureSpec), MeasureSpecMode.Exactly);
    
                base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
            }
    
        }
    }
    

    Hope this helps someone.

  • deckertron9000deckertron9000 USUniversity ✭✭✭

    The above "fix" did not work in my case. I did manage to track down a cause for anyone else still seeing this issue. If you call SetPage( aMasterDetailPage ) while there's a page on-screen showing a navigation bar, you will experience this exception. If you call SetPage( aMasterDetailPage ) without a navigation bar showing on-screen, everything is butterflies and rainbows.

    Hope this helps someone in the meantime. Also updated my bugzilla task: here

  • hrafnlhrafnl USMember

    How do you then remove the on-screen page (which is in my case a page showing a navigation bar)? I can PopToRootAsync() but then I just pop to the root page which is a navigation page and still have the same problem.

  • deckertron9000deckertron9000 USUniversity ✭✭✭

    @hrafnl‌
    I used the NavigationPage.SetHasNavigationBar(Page page, bool value) method on the Page to hide the navigation bar before navigating. It seems like Xamarin.Forms 1.3.0 will implement SetPage() in a much better way. No idea when it fully releases though.

  • hrafnlhrafnl USMember

    @deckertron9000‌: I tried this without luck. Here is what I'm doing in general:

    My app starts with this method in the App class:

    public static Page GetLoginPage(ILoginManager ilm )
    {
                MyNavigationPage = new NavigationPage(new WelcomePage(ilm))
                {
                    BarTextColor = Color.White,
                    BarBackgroundColor = Color.FromHex("c1122e"),
                };
                return MyNavigationPage;
    }
    

    From the welcome page the user navigates to a login page. Once logged in, the following is executed:

    ilm.ShowMainPage ();

    This fires a method implemented in each platform. On the iOS (AppDelegate.cs) it does:

    public void ShowMainPage()
    {
                window.RootViewController = App.GetMainPage().CreateViewController();
                window.MakeKeyAndVisible();
    }
    

    The GetMainPage opens a Master-Detail page and works as intended in iOS.

    On Android (MainActivity.cs) it does:

    public void ShowMainPage()
    {
                SetPage(App.GetMainPage());
    }
    

    but crashes.

    I tried adding the following before ilm.ShowMainPage():

    NavigationPage.SetHasNavigationBar(this, false);   // Remove navigation bar from the login page
    Navigation.PopToRootAsync();        
    NavigationPage.SetHasNavigationBar(App.MyNavigationPage.CurrentPage, false);  // Remove navigation bar from the welcome page
    

    Again, this works fine on iOS, but still crashes on Android.

    Any clue what is going on here?

  • deckertron9000deckertron9000 USUniversity ✭✭✭

    @hrafnl‌
    I'll try to take a closer look after work today. Just out of curiosity, what version of Xamarin.Forms are you using?
    Also, what's your output look like after crash? This message in there?

    Java.Lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.

  • hrafnlhrafnl USMember

    @deckertron9000‌ Thanks for the help!

    Xamarin.Forms version: 1.2.3.6257

    Output after crash:
    --- End of managed exception stack trace ---
    12-10 16:52:04.229 E/mono (11968): java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
    12-10 16:52:04.229 E/mono (11968): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:679)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.measure(View.java:15513)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.n_onLayout(Native Method)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.onLayout(PlatformRenderer.java:54)
    ...

  • deckertron9000deckertron9000 USUniversity ✭✭✭

    @hrafnl‌
    I looked through it some more and I decided it's far easier to upgrade to Xamarin.Forms 1.3 -pre2. They seemed to have fixed whatever issue was causing this Layout measurement exception.

  • hrafnlhrafnl USMember
    edited December 2014

    Thanks @deckertron9000‌.

    A silly question: How do I upgrade to a prerelease?

    Edited:

    In Package Manager Console:
    Install-Package Xamarin.Forms -Version 1.3.0.6280-pre2 -Pre

    And after this upgrade my Master-Detail in Android does not crash!

  • deckertron9000deckertron9000 USUniversity ✭✭✭

    @hrafnl‌ Excellent! Glad the prerelease cleared things up for you!

  • hrafnlhrafnl USMember

    @deckertron9000‌: Thanks for the help!

    Xamarin.Forms version: 1.2.3.6257

    Output after crash:

    --- End of managed exception stack trace ---
    12-10 16:52:04.229 E/mono (11968): java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
    12-10 16:52:04.229 E/mono (11968): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:679)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.measure(View.java:15513)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.n_onLayout(Native Method)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.onLayout(PlatformRenderer.java:54)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1652)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)

  • hrafnlhrafnl USMember

    @deckertron9000‌ Thanks for the help!

    Xamarin.Forms version: 1.2.3.6257

    Output after crash:
    --- End of managed exception stack trace ---
    12-10 16:52:04.229 E/mono (11968): java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
    12-10 16:52:04.229 E/mono (11968): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:679)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.measure(View.java:15513)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.n_onLayout(Native Method)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.onLayout(PlatformRenderer.java:54)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1652)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)

  • hrafnlhrafnl USMember

    @deckertron9000‌: Thanks for the help!

    Xamarin.Forms version: 1.2.3.6257

    Output after crash:

    --- End of managed exception stack trace ---
    12-10 16:52:04.229 E/mono (11968): java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
    12-10 16:52:04.229 E/mono (11968): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:679)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.measure(View.java:15513)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.n_onLayout(Native Method)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.onLayout(PlatformRenderer.java:54)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1652)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)

  • hrafnlhrafnl USMember

    @deckertron9000‌ Thanks for the help!

    Xamarin.Forms version: 1.2.3.6257

    Output after crash:
    --- End of managed exception stack trace ---
    12-10 16:52:04.229 E/mono (11968): java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
    12-10 16:52:04.229 E/mono (11968): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:679)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.measure(View.java:15513)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.n_onLayout(Native Method)
    12-10 16:52:04.229 E/mono (11968): at xamarin.forms.platform.android.PlatformRenderer.onLayout(PlatformRenderer.java:54)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1652)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
    12-10 16:52:04.229 E/mono (11968): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    12-10 16:52:04.229 E/mono (11968): at android.view.View.layout(View.java:14003)
    12-10 16:52:04.229 E/mono (11968): at android.view.ViewGroup.layout(ViewGroup.java:4375)

Sign In or Register to comment.