应用Material Theme时Xamarin.Forms空引用异常 [英] Xamarin.Forms null reference exception when applying Material Theme
问题描述
遵循您的Xamarin的材料设计.在Xamarin网站上的Forms Android Apps 教程中,我的项目正在认识到style.xml的存在并将其应用于应用程序的初始启动屏幕":
Following the Material Design for Your Xamarin.Forms Android Apps tutorial on the Xamarin website, my project is recognising the existence of my style.xml and applying it to the starting "splash screen" of the application:
但是,一旦应用程序加载我的MainPage,Visual Studio就会为我提供一个空引用异常,除了在我的应用程序实例化中发生之外,没有其他详细信息.当删除对主题的引用时,该应用程序确实会启动并按预期运行.我仅使用本教程中包含的示例style.xml和color.xml文件,并且从主题中删除实际样式仍然会使应用程序崩溃.
However as soon as the application loads my MainPage I am given a null reference exception by Visual Studio with no other details besides it occurring in the instantiation of my application. When removing references to the theme, the application does start and runs as expected. I am only using the example style.xml and color.xml files as included in the tutorial and removing the actual styling from the theme still crashes the application.
style.xml
style.xml
<resources>
<style name="MyTheme" parent="MyTheme.Base"></style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:windowBackground">@color/window_background</item>
<item name="windowActionModeOverlay">true</item>
</style>
</resources>
colors.xml
colors.xml
<resources>
<color name="primary"> #1976D2</color>
<color name="primaryDark"> #FFC107</color>
<color name="accent">#F5F5F5</color>
<color name="window_background">#2196F3</color>
</resources>
MainActivity.cs
MainActivity.cs
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Ripper_Quotes_Forms.Droid
{
[Activity (Label = "Ripper Quotes", Icon = "@android:color/transparent", Theme="@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new Ripper_Quotes_Forms.App ());
}
}
}
MainPage.xaml
MainPage.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:local="clr-namespace:Ripper_Quotes_Forms"
x:Class="Ripper_Quotes_Forms.MainPage"
Title="Ripper Quotes">
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListView x:Name="QuoteList" Grid.Row="0">
<ListView.ItemTemplate>
<DataTemplate>
<local:QuoteViewCell/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</ContentPage.Content>
</ContentPage>
MainPage.xaml.cs
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using RipperQuotes;
namespace Ripper_Quotes_Forms
{
public partial class MainPage : ContentPage
{
private RipperCon _con = new RipperCon();
public MainPage()
{
InitializeComponent();
QuoteList.HasUnevenRows = true;
NavigationPage.SetTitleIcon(this, "");
ToolbarItems.Add(new ToolbarItem("New", "", async () =>
await Navigation.PushModalAsync(new NewQuotePage())
));
ToolbarItems.Add(new ToolbarItem("More", "", async () =>
await DisplayActionSheet("More", "Back", "", new string[] { "View Categories" })
));
}
protected async override void OnAppearing()
{
base.OnAppearing();
await _con.DownloadJson();
QuoteList.ItemsSource = _con.Quotes;
}
}
}
推荐答案
似乎您错过了一步.
查看MainActivity
中的行public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
.应用Material Theme时,它应继承自FormsAppCompatActivity
.还要注意他是如何在OnCreate
中添加这两行的:
Look at the line public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
in your MainActivity
. When applying the Material Theme it should inherit from FormsAppCompatActivity
. Also notice how he adds these two lines in ther OnCreate
:
FormsAppCompatActivity.ToolbarResource = Resource.Layout.toolbar;
FormsAppCompatActivity.TabLayoutResource = Resource.Layout.tabs;
查看您为本教程提供的链接,然后再次通读以再次检查您没有错过任何内容.
Look into the link you supplied yourself for the tutorial and read through it again to double-check you didn't miss anything.
这篇关于应用Material Theme时Xamarin.Forms空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!