xamarin形成appcompat nullreference异常

时间:2017-12-01 09:50:13

标签: xamarin xamarin.forms xamarin.android android-appcompat

我在这一行得到一个NullReferenceException:

LoadApplication(new App());

在我的主要活动中。 我正在处理的是在thisthis博客帖子之后添加Compat主题以获得this徽章插件。

[Activity(Label = "ServiceApp.Droid", 
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

public class MainActivity : FormsAppCompatActivity {
    protected override void OnCreate(Bundle bundle) {
        FormsAppCompatActivity.ToolbarResource = Resource.Layout.Toolbar;
        FormsAppCompatActivity.TabLayoutResource = Resource.Layout.Tabbar;
        base.OnCreate(bundle);
        [...]
        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    } 
    [...]
}

资源看起来像这样: 资源/值/ color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="primary">#FFDD00</color>
  <color name="primaryDark">#FFDD00</color>
  <color name="primaryLight">#FFECB3</color>
  <color name="accent">#9E9E9E</color>
  <color name="primaryText">#212121</color>
  <color name="secondaryText">#757575</color>
  <color name="window_background">#FFFFFF</color>
  <color name="icons">#212121</color>
  <color name="divider">#BDBDBD</color>
</resources>

资源/值/ styles.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <style name="MyTheme" parent="MyTheme.Base">
  </style>
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <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>

资源/值-V21 / styles.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="MyTheme" parent="MyTheme.Base">

    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
  </style>
</resources>

另外 资源/布局/ Tabbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:tabIndicatorColor="@android:color/white"
    app:tabGravity="fill"
    app:tabMode="fixed" />

和 资源/布局/ Toolbar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_scrollFlags="scroll|enterAlways" />

我错过了有关主题的内容吗?或者有没有办法获得有关异常的更多信息? (检查了android traces.txt,这似乎没有帮助) 我通过单步执行LoadApplication(新的App())来跟踪它;它似乎在最后一步崩溃了,但我不知道实际上是什么原因导致这个问题。

1 个答案:

答案 0 :(得分:0)

如果您仍有问题,请查看此帖子: Xamarin Forms MainActivity OnCreate LoadApplication System.NullReferenceException: Object reference not set to an instance of an object

一般情况下,您缺少活动定义中的主题

[Activity(Label = "ServiceApp.Droid", 
Theme = "@style/MyTheme",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]