不幸的是,MyApp.Android已经停止了。(Unfortunately, MyApp.Android has stopped. with ContentPage.Resource in XAML

编程入门 行业动态 更新时间:2024-10-23 04:47:07
不幸的是,MyApp.Android已经停止了。(Unfortunately, MyApp.Android has stopped. with ContentPage.Resource in XAML file)

我开始使用Xamarin Forms在Android上构建UI。我在下面给出了XAML文件。

<?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>

我能够编译并部署到设备中。当我从设备运行应用程序时,我收到错误说Unfortunately, MyApp.Android has stopped.

如果我注释掉<ContentPage.Resources>标签它会运行并向我显示一个空白屏幕。 为什么错了

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

logcat显示以下消息。

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

如果我在logcat中查看Android Runtime ,我会收到以下错误。

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

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

最满意答案

ContentPage.Resources需要一个ResourceDictionary实例,但xaml会为它提供一个NullVisibilityConverter实例,导致类型不匹配。

通过将NullVisibilityConverter包装在资源字典中来修复它:

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

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>

更多推荐

本文发布于:2023-08-07 18:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:的是   不幸   停止了   MyApp   XAML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!