liv2hak liv2hak - 28 days ago 20
Android Question

Unfortunately, MyApp.Android has stopped. with ContentPage.Resource in XAML file

I am starting to build up a UI on Android using Xamarin Forms.My

XAML
file is give below.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:PortableViews="clr-namespace:MyApp.Portable.Views"
x:Class="MyApp.Portable.Views.MainPage3">

<ContentPage.Resources>
<PortableViews:NullVisibilityConverter x:Key="NullVisibilityConverter"/>
</ContentPage.Resources>
<Grid>
</Grid>
</ContentPage>


I am able to compile and deploy into the device.When I run the app from the device I get the error saying
Unfortunately, MyApp.Android has stopped.


If I comment out
<ContentPage.Resources>
tag It runs and shows me a blank screen. Why is wrong with

<ContentPage.Resources>
<PortableViews:NullVisibilityConverter x:Key="NullVisibilityConverter"/>
</ContentPage.Resources>


logcat shows the following message.

11-10 14:01:39.675 I/DEBUG ( 2131): [OnPurpose Redunant in void preset_info(aed_report_record*, int, int)] pid: 2058, tid: -1361051648, name: UNKNOWN >>> MyApp.Android.MyApp.Android


If I look at
Android Runtime
in logcat I get the following errors.

11-10 14:42:18.554 E/AndroidRuntime( 4487): FATAL EXCEPTION: main
11-10 14:42:18.554 E/AndroidRuntime( 4487): Process: MyApp.Android.MyApp.Android, PID: 4487
11-10 14:42:18.554 E/AndroidRuntime( 4487): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
11-10 14:42:18.554 E/AndroidRuntime( 4487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
11-10 14:42:18.554 E/AndroidRuntime( 4487): Caused by: java.lang.reflect.InvocationTargetException
11-10 14:42:18.554 E/AndroidRuntime( 4487): at java.lang.reflect.Method.invoke(Native Method)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at java.lang.reflect.Method.invoke(Method.java:372)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
11-10 14:42:18.554 E/AndroidRuntime( 4487): ... 1 more
11-10 14:42:18.554 E/AndroidRuntime( 4487): Caused by: android.runtime.JavaProxyThrowable: Xamarin.Forms.Xaml.XamlParseException: Position 13:6. Type Window not found in xmlns http://xamarin.com/schemas/2014/forms
11-10 14:42:18.554 E/AndroidRuntime( 4487): at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.GetRealNameAndType (System.Type& elementType, System.String namespaceURI, System.String& localname, Xamarin.Forms.Xaml.HydratationContext context, IXmlLineInfo lineInfo) <0x7f59f99ac0 + 0x0013c> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue (System.Object xamlelement, XmlName propertyName, System.Object value, Xamarin.Forms.BindableObject rootElement, INode node, Xamarin.Forms.Xaml.HydratationContext context, IXmlLineInfo lineInfo) <0x7f59fb3160 + 0x0013b> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, INode parentNode) <0x7f59f9e048 + 0x005eb> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at Xamarin.Forms.Xaml.ElementNode.Accept (IXamlNodeVisitor visitor, INode parentNode) <0x7f59fc4658 + 0x00277> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at Xamarin.Forms.Xaml.RootNode.Accept (IXamlNodeVisitor visitor, INode parentNode) <0x7f59fc3ed8 + 0x000ff> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at Xamarin.Forms.Xaml.XamlLoader.Load (Xamarin.Forms.BindableObject view, System.String xaml) <0x7f5a924650 + 0x00387> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at Xamarin.Forms.Xaml.XamlLoader.Load (Xamarin.Forms.BindableObject view, System.Type callingType) <0x7f5a99ba20 + 0x00053> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TView] (Xamarin.Forms.Xaml.TView view, System.Type callingType) <0x7f5a99b9d8 + 0x00023> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at MyApp.Portable.Views.MainPage3.InitializeComponent () <0x7f5a99b950 + 0x0002b> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at MyApp.Portable.Views.MainPage3..ctor () <0x7f5a9bf0d0 + 0x0001f> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at MyApp.Portable.App..ctor () <0x7f5b4276f8 + 0x0002f> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at MyApp.Android.MainActivity.OnCreate (Android.OS.Bundle bundle) <0x7f5b457370 + 0x00043> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) <0x7f5b457080 + 0x00063> in <filename unknown>:0
11-10 14:42:18.554 E/AndroidRuntime( 4487): at (wrapper dynamic-method) System.Object:5c4437ec-ddbf-41de-b890-3c31365af092 (intptr,intptr,intptr)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at md5b10bea1896ca4a98de5581931a9e1765.MainActivity.n_onCreate(Native Method)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at md5b10bea1896ca4a98de5581931a9e1765.MainActivity.onCreate(MainActivity.java:29)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at android.app.Activity.performCreate(Activity.java:5971)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at android.app.ActivityThread.access$800(ActivityThread.java:167)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1417)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at android.os.Handler.dispatchMessage(Handler.java:111)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at android.os.Looper.loop(Looper.java:194)
11-10 14:42:18.554 E/AndroidRuntime( 4487): at android.app.ActivityThread.main(ActivityThread.java:5534)
11-10 14:42:18.554 E/AndroidRuntime( 4487): ... 4 more

Answer

ContentPage.Resources expects a ResourceDictionary instance but that xaml will provide it with an instance of NullVisibilityConverter instead, resulting in a type-mismatch.

Fix it by wrapping the NullVisibilityConverter inside a resource dictionary:

<ContentPage.Resources>
    <ResourceDictionary>
         <PortableViews:NullVisibilityConverter x:Key="NullVisibilityConverter"/>
    </ResourceDictionary>
</ContentPage.Resources>