应用Material Theme时,Xamarin.Forms为null引用异常

时间:2016-12-11 01:32:58

标签: c# android xml xamarin.forms

在Xamarin网站上的Material Design for Your Xamarin.Forms Android Apps教程之后,我的项目认识到我的style.xml的存在并将其应用到起始"启动画面"申请:

但是,只要应用程序加载我的MainPage,我就会被Visual Studio给出一个空引用异常,除了它在我的应用程序实例化中发生之外没有其它细节。删除对主题的引用时,应用程序会启动并按预期运行。我只使用教程中包含的示例style.xml和color.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

<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

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中

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

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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

看来你错过了一步。

查看public class InputParam { private int fieldOne; private string fieldTwo; ... GetFieldOne(); GetFieldTwo(); ... } 中的public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity行。应用材质主题时,它应该从MainActivity继承。还要注意他如何在FormsAppCompatActivity中添加这两行:

OnCreate

查看您自己为本教程提供的链接,并再次仔细阅读以仔细检查您是否错过了任何内容。