【问题标题】:Xamarin - AppCompat does not support the current theme featuresXamarin - AppCompat 不支持当前主题功能
【发布时间】:2015-09-29 16:12:45
【问题描述】:

我注意到这是一个非常常见的问题,但没有一个解决方案对我有用。这是大家似乎都参考的主线:

Upgraded to AppCompat v22.1.0 and now getting IllegalArgumentException: AppCompat does not support the current theme features

但我试过没有运气。我错过了什么吗??

这是我的清单

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
        <uses-sdk />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    </manifest>

这是我的 styles.xaml

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">

    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <!-- Both of these are needed -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

这是我的基础活动

    public abstract class ActivityBase : MvxCachingFragmentActivityCompat
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Make this activity fullscreen
            this.Window.AddFlags(WindowManagerFlags.Fullscreen);
        }

        .
        .
        .
    }

这是我的示例活动

    [Activity(Label = "Add Child", Theme = "@style/AppTheme")]
    public class AddChildPage : ActivityBase, DatePickerDialog.IOnDateSetListener
    {
        .
        .
        .
    }

我基本上是直接在我正在测试的活动上设置主题。我没有在清单或基本活动中设置主题。

我是不是做错了什么?

更新:

按照这些步骤操作后,我很高兴错误消失了,但现在我收到了一个新错误,这是我在引入 AppCompat 基础活动之前从未遇到过的:

 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddingLeft()' on a null object reference

我会对此进行更多调查,如果我没有找到答案,我会将 Daniele 的答案标记为有效,因为它确实解决了我之前的错误。

【问题讨论】:

    标签: android android-layout android-activity xamarin xamarin.android


    【解决方案1】:

    我遇到了同样的问题,我是这样解决的。

    在清单中你应该添加:

    <application android:label="yourApp" android:theme="@style/AppBaseTheme"></application>
    

    你的基础活动应该扩展

    :AppCompatActivity

    你必须删除 这部分

    // 使这个活动全屏 this.Window.AddFlags(WindowManagerFlags.Fullscreen);

    也从你的所有活动中删除

    主题 = "@style/AppTheme"

    (让他们变成这样)

    [Activity(Label = "Add Child")]

    现在,您的 styles.xaml 应该是:

        <resources>
      <style name="AppBaseTheme" parent="AppBaseTheme.Base">
      </style>
      <!-- Base theme applied no matter what API -->
      <style name="AppBaseTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
        <item name="windowNoTitle">true</item>
        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="windowActionBar">false</item>
        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">#2196F3</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">#1976D2</item>
        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="colorAccent">#40FF81</item>
        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight and colorSwitchThumbNormal. -->
      </style>
    </resources>
    

    但您还需要(这是必需的,否则 Appcompat 将不起作用)在 Resources 下创建一个名为 values-v21 的新文件夹,该文件夹只能从 Android 5+ 的设备中看到,并且在其中您将拥有另一个包含此内容的 styles.xaml :

    <resources>
      <style name="AppTheme" parent="AppTheme.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>
    

    请注意,样式名称和父样式文件需要与其他样式文件完全相同,并且在清单中声明的​​相同。

    现在您需要直接从支持库中引用新的工具栏小部件。如果您尚未在 Resources/Layout/toolbar.axml 下创建新的 Android 布局文件,请执行此操作。该文件将如下所示:

    <?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="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
    

    使用工具栏设置,您现在可以使用包含节点将工具栏集成到您的布局中。 我知道,以前没有必要在每个布局中包含操作栏,但没有什么可做的:App Compat 要求您在活动布局中包含工具栏。 这是一个例子:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/main_content"
            android:layout_below="@id/toolbar">
        </LinearLayout>
    </RelativeLayout>
    

    您不应该更改的重要部分(或使用您的 id 和布局工具栏.xaml 进行相应更改)是这个:

    <include    android:id="@+id/toolbar"   layout="@layout/toolbar" />
    

    我们差不多完成了: 在您的所有活动中添加:

    using Toolbar = Android.Support.V7.Widget.Toolbar;
    

    你的 OnCreate 方法应该像这样开始

    protected override void OnCreate (Bundle bundle)
    {
      base.OnCreate (bundle);
      // Set our view from the "main" layout resource
      SetContentView (Resource.Layout.main);
      var toolbar = FindViewById<Toolbar> (Resource.Id.toolbar);
      //Toolbar will now take on default actionbar characteristics
      SetSupportActionBar (toolbar);
      SupportActionBar.Title = "Hello from stackoverflow";
      //..
    }
    

    只有在设置了 ContentView 之后才能设置工具栏。

    这些步骤对我有用,并帮助我更新到 AppCompatv7 r22.2 据我所知,AppCompatv7 r22.1 有一些小错误。 从 9 月 29 日开始,还有一个新版本的 AppCompat v7 (r.23)

    【讨论】:

    • 哇,没想到有这么多步骤 :) 好的,我会尽快尝试并报告。谢谢丹尼尔!!
    • 是的,有点长,我想我可以跳过一些步骤,但我一直在努力,直到我完成了所有步骤。祝你好运!
    • 不幸的是得到一个新的错误,但我会看看我能不能找到它。但我认为它奏效了。
    • 如果您愿意,请随时分享错误。也许有人可以帮忙。
    • 终于,它成功了!!我的问题与我得到 NullRefExceptions 的事实有关,因为我在 OnViewModelSet 覆盖 (MvvmCross) 上夸大了我的观点,而且似乎在切换到使用 AppCompat 之后,事件在 OnCreate 之前被触发。感谢您的所有帮助丹尼尔!
    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 2017-08-07
    • 2018-01-20
    • 2015-11-16
    • 2017-12-08
    • 2015-06-29
    相关资源
    最近更新 更多