【问题标题】:Getting a "You need to use a Theme.AppCompat theme (or descendant) with this activity" error in Xamarin在 Xamarin 中出现“您需要在此活动中使用 Theme.AppCompat 主题(或后代)”错误
【发布时间】:2020-06-26 00:37:24
【问题描述】:

我收到以下错误

You need to use a Theme.AppCompat theme (or descendant) with this activity

我正在学习 PluralSight 课程,但我认为我错过了某个关键步骤,但经过两天的摸索,我似乎找不到明显的地方。

这是我的代码

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          android:versionCode="1"
          android:versionName="1.0"
          package="com.companyname.bethanypieshopmobile"
          android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
    <application android:allowBackup="true" 
               android:icon="@mipmap/ic_launcher" 
               android:label="@string/app_name" 
               android:roundIcon="@mipmap/ic_launcher_round" 
               android:supportsRtl="true" 
               android:theme="@style/BethanysTheme"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
</manifest>

Activity.cs

using Android.App;
using Android.OS;
using Android.Support.V4.View;
using Android.Support.V7.App;
using BethanyPieShopMobile.Adapters;

namespace BethanyPieShopMobile
{
    [Activity(Label = "PieMenuWithTabsActivity")]
    public class PieMenuWithTabsActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            //the line below throws the error
            SetContentView(Resource.Layout.pie_menu_tabs);
            var viewPager = FindViewById<ViewPager>(Resource.Id.piePager);
            var categoryFragmentAdapter = new CategoryFragmentAdapter(SupportFragmentManager);
            viewPager.Adapter = categoryFragmentAdapter;
        }
    }
}

styles.xml

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="BethanysTheme" parent="android:Theme.Material.Light">
    <item name="android:colorPrimary">@color/bethanyGreen</item>
    <item name="android:colorAccent">@color/bethanyBeige</item>
  </style>

  <style name="BethanyGreenText" parent="TextAppearance.AppCompat">
    <item name="android:textColor">#23cfa7</item>
  </style>

</resources>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/piePager" >

    <android.support.v4.view.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="12dp"
        android:paddingTop="10dp"
        android:textColor="#000000"/>

</android.support.v4.view.ViewPager>

【问题讨论】:

    标签: android xamarin xamarin.android


    【解决方案1】:

    为 MainActivity 添加主题属性

    [Activity(Label = "", MainLauncher = true, Theme = "@style/AppTheme")]
    

    在清单中更改主题

    android:theme="@style/MainTheme"
    

    在styles.xml中设置BethanysTheme的parent="Theme.AppCompat.Light.NoActionBar"

    这些链接会帮助你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-15
      • 2018-11-27
      • 2015-11-27
      • 2016-07-04
      • 2016-05-16
      相关资源
      最近更新 更多