【问题标题】:xamarin forms appcompat nullreference exceptionxamarin 表单 appcompat nullreferenceexception
【发布时间】:2018-04-19 15:25:20
【问题描述】:

我在这一行收到 NullReferenceException:

加载应用程序(新应用程序());

在我的主要活动中。 在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>

资源/values-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(new App());它似乎在最后一步崩溃了,但我不知道导致问题的实际是什么。

【问题讨论】:

  • 您是否提供了您的应用程序类,即您的 app.xaml 主页???
  • 是的,我确实有一个主页。我应该注意到,该应用在尝试迁移到 AppCompat 之前确实可以运行。
  • 所以问题是在移到 appcompact 后开始的?
  • 是的,之前没有出现这个错误。
  • 当它崩溃时它说通货膨胀错误是什么?还是什么?

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


【解决方案1】:

如果您仍然有问题,请查看此帖子: 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)]

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 1970-01-01
    • 2017-03-07
    • 2022-11-21
    • 1970-01-01
    • 2016-08-06
    • 2017-03-26
    • 2016-06-12
    • 2023-03-22
    相关资源
    最近更新 更多