gpt4 book ai didi

android - MvvmCross 和 Xamarin Forms,Android 应用程序在获取强调色时因空引用错误而崩溃

转载 作者:行者123 更新时间:2023-11-29 19:10:00 24 4
gpt4 key购买 nike

我使用 MvvmCross 创建了一个简单的 Xamarin Forms 应用程序,以尝试找出我在另一个项目中遇到的错误。这个简单的应用程序可以在这里找到:https://github.com/selaromdotnet/XamFormsAppTest

它有一个单独的页面,上面有一个工具栏项,上面有一个图标。

如果您查看提交历史记录,这个项目加载得很好,图标也是如此。

但在最近的提交中,我按照此处的指南 (https://blog.xamarin.com/material-design-for-your-xamarin-forms-android-apps/) 和此处的这篇文章 (Xamarin.Forms null reference exception when applying Material Theme) 更新项目以将 MvvmCross 与 Material Design 结合使用。

现在在 android 应用程序上,MvxFormsAppCompatActivity 的 OnCreate 方法崩溃并出现此错误:

 System.NullReferenceException: Object reference not set to an instance of an object.
at Xamarin.Forms.Forms.GetAccentColor () [0x00006] inC:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Forms.cs:159
at Xamarin.Forms.Forms.SetupInit (Android.Content.Context activity, System.Reflection.Assembly resourceAssembly) [0x0000c] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Forms.cs:118
at Xamarin.Forms.Forms.Init (Android.Content.Context activity, Android.OS.Bundle bundle) [0x00006] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Forms.cs:72
at MvvmCross.Forms.Droid.MvxFormsAndroidSetup.CreateViewPresenter () [0x00000] in C:\projects\mvvmcross\MvvmCross-Forms\MvvmCross.Forms.Droid\MvxFormsAndroidSetup.cs:55
at MvvmCross.Droid.Platform.MvxAndroidSetup.CreateViewDispatcher () [0x00000] in C:\projects\mvvmcross\MvvmCross\Droid\Droid\Platform\MvxAndroidSetup.cs:124
at MvvmCross.Core.Platform.MvxSetup.InitializeViewDispatcher () [0x00000] in C:\projects\mvvmcross\MvvmCross\Core\Core\Platform\MvxSetup.cs:260
at MvvmCross.Core.Platform.MvxSetup.InitializeSecondary () [0x000f8] in C:\projects\mvvmcross\MvvmCross\Core\Core\Platform\MvxSetup.cs:91
at MvvmCross.Core.Platform.MvxSetup.Initialize () [0x00006] in C:\projects\mvvmcross\MvvmCross\Core\Core\Platform\MvxSetup.cs:37
at MvvmCross.Droid.Platform.MvxAndroidSetupSingleton.EnsureInitialized () [0x0004e] in C:\projects\mvvmcross\MvvmCross\Droid\Droid\Platform\MvxAndroidSetupSingleton.cs:45
at MvvmCross.Forms.Droid.MvxFormsAppCompatActivity.OnCreate (Android.OS.Bundle bundle) [0x00013] in C:\projects\mvvmcross\MvvmCross-Forms\MvvmCross.Forms.Droid\MvxFormsAppCompatActivity.cs:64
at XamFormsAppTest.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00017] in D:\VisualStudio\OSS\XamFormsAppTest\XamFormsAppTest\XamFormsAppTest.Android\MainActivity.cs:21
at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_(System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in 8b5e0f4c6f594871ab0afcbcb2317289>:0
at (wrapper dynamic-method) System.Object:6ce77b2f-ee95-4ad0-bd89-6baa2e4d7b05> (intptr,intptr,intptr)

我猜它没有在资源中正确找到颜色,但我不知道如何解决这个问题。我尝试将文件名更改为 color.xml 并尝试将 accentColor 定义为颜色而不是对 @color/accent 的引用...

我真的被困在这里,不知道如何解决这个问题。谁能告诉我我的项目做错了什么?

最佳答案

Object reference not set to an instance of an object.

首先,请确保您已阅读 Official tutorial 使用 Xamarin.Forms 创建一个 MvvmCross 项目。这是一个sample关于如何创建一个 MvvmCross Forms 项目。请根据文档修改您的MainPage.xaml代码。

我们可以发现 NullReferenceException 有一个顺序:

MainActivity.OnCreate --> MvxFormsAppCompatActivity.OnCreate --> .. --> Init --> SetupInit --> GetAccentColor

当系统调用MainActivity.OnCreate方法时为null,所以我们可以推断在资源中没有正确找到颜色,因为你的MainActivity没有被初始化.我们应该在准备好后调用 MainActivityOnCreate 方法。看来我们必须添加一个SplashScreen 来初始化一些资源。

这是我的代码:

[Activity(
Label = "MvxForms"
, MainLauncher = true
, Icon = "@drawable/icon"
, NoHistory = true
, ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashScreen : MvxSplashScreenActivity
{
public SplashScreen()
: base(Resource.Layout.SplashScreen)
{
}

protected override void TriggerFirstNavigate()
{
StartActivity(typeof(MainActivity));
base.TriggerFirstNavigate();
}
}

然后,你的 NullReferenceException 将消失,但在这样做之后,它仍然有一个 Exception :

 FATAL UNHANDLED EXCEPTION: MvvmCross.Platform.Exceptions.MvxException:
Problem seen creating View-ViewModel lookup table
- you have more than one View registered for the ViewModels: 2*MainViewModel (MainActivity,MainPage)
---> System.ArgumentException: An item with the same key has already been added.

If your ViewModel is called MainViewModel and your Forms page is too, you might get a name conflict because MvvmCross will have 2 view to viewmodel lookups. You can prevent this by naming your Activity differently like "FormsActivity.cs".

你也可以将你的 MainViewModel 重命名为 MvxMainViewModel(随便你喜欢什么),然后这个异常就会消失。

关于android - MvvmCross 和 Xamarin Forms,Android 应用程序在获取强调色时因空引用错误而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45686743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com