首页 文章

您需要在此活动中使用Theme.AppCompat主题(或后代)

提问于
浏览
827

Android Studio 0.4.5

用于创建自定义对话框的Android文档:http://developer.android.com/guide/topics/ui/dialogs.html

如果需要自定义对话框,则可以将“活动”显示为对话框,而不是使用“对话框API” . 只需创建一个活动并将其主题设置为 <activity> 清单元素中的Theme.Holo.Dialog:

<activity android:theme="@android:style/Theme.Holo.Dialog" >

但是,当我尝试这个时,我得到以下异常:

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

我支持以下内容,我不能使用大于10的内容:

minSdkVersion 10
targetSdkVersion 19

在我的风格中,我有以下内容:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

在我的清单中,我有这个活动:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:theme="@android:style/Theme.Holo.Light.Dialog"
            android:name="com.ssd.register.Dialog_update"
            android:label="@string/title_activity_dialog_update" >
        </activity>

像我这样创建对话框是我要做的事情,因为我已经完成了布局 .

谁能告诉我如何解决这个问题?

30 回答

  • 24

    我遇到了这个问题,即使我的主题是 AppCompat 主题,我的活动是 AppCompatActivity (或 Activity ,如其他人的答案所示) . 所以我清理,重建并重新运行项目 .

    Build -> Clean Project ; Build -> Rebuild Project ; Run -> Run

    它可能看起来很愚蠢,但现在它 works great

    希望它有所帮助!

  • 7

    您遇到此问题的原因是您尝试应用对话框主题的活动正在扩展 ActionBarActivity ,这需要应用 AppCompat 主题 .

    Update :扩展 AppCompatActivity 也会出现此问题

    在这种情况下,将Java继承从 ActionBarActivity 更改为 Activity ,并将对话框主题保留在清单中,非 Theme.AppCompat


    一般规则是,如果您希望您的代码支持旧版本的Android,它应该具有 AppCompat 主题,并且Java代码应该扩展 AppCompatActivity . 如果您有一个不需要此支持的活动,例如您只关心Android的最新版本和功能,您可以应用任何主题,但java代码必须扩展旧的 Activity .


    注意:当从 AppCompatActivity (或子类, ActionBarActivity )更改为 Activity 时,还必须将"support"的各种调用更改为不带"support"的相应调用 . 所以,而不是 getSupportFragmentManager ,请调用 getFragmentManager .

  • 484

    您需要做的就是将 android:theme="@style/Theme.AppCompat.Light" 添加到 AndroidManifest.xml 文件中的应用程序标记中 .

  • 5

    在上面的评论中从@MarkKeen复制答案,因为我遇到了同样的问题 .

    我在帖子顶部说明了错误,并在我添加了一个警告对话框后发生了 . 我在清单中有所有相关的样式信息 . 通过更改警报生成器中的上下文引用来解决我的问题 - 我更改了:

    new android.support.v7.app.AlertDialog.Builder(getApplicationContext())
    

    至:

    new android.support.v7.app.AlertDialog.Builder(this)
    

    没有更多的问题 .

  • 11

    min sdk是10. ActionBar 可从api级别11获得 . 因此对于10,您将使用支持库中的 AppCompat ,您需要使用 Theme.AppCompat 或其后代 .

    使用

    android:theme="@style/Theme.AppCompat" >
    

    或者,如果你不想在顶部的动作栏

    android:theme="@style/Theme.AppCompat.NoActionBar">
    

    更多信息 @

    http://developer.android.com/guide/topics/ui/actionbar.html

    编辑:

    我可能误读了op post .

    似乎操作需要具有活动主题的Dialog . 因此,正如Bobbake4已经建议的那样,扩展 Activity 而不是 ActionBarActivity .

    还可以在下面的链接中查看@ Dialog Attributes

    http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/values/themes.xml/

  • 198

    这对我有用,我刚刚改变了:

    final AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
    

    对此:

    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    
  • 5

    这就是为我解决的问题:我没有在清单中指定主题,而是在 onCreate 中为每个扩展 ActionBarActivity 的活动定义了它:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.MyAppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity_layout);
    ...
    }
    

    这里 MyAppThemeTheme.AppCompat 的后代,并以xml定义 . 请注意,必须在 super.onCreatesetContentView 之前设置主题 .

  • 3

    去你的风格并把父母

    parent="Theme.AppCompat"
    

    代替

    parent="@android:style/Theme.Holo.Light"
    
  • 5

    在我的情况下,我的res目录中没有values-v21文件 . 然后我创建它并在其中添加以下代码:

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

    如果需要在style.xml上扩展ActionBarActivity:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base"/>
    
    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    

    如果你将你的应用程序的主题设置为android:Theme.Material.Light而不是 AppTheme.Base 那么你将得到一个“IllegalStateException:你需要使用一个Theme.AppCompat主题(或后代)与此活动”错误 .

  • 3

    更改所需活动的主题 . 这对我有用:

    <activity
                android:name="HomeActivity"
                android:screenOrientation="landscape"
                android:theme="@style/Theme.AppCompat.Light"
                android:windowSoftInputMode="stateHidden" />
    
  • 13

    我有同样的问题,但当我把它放在清单上时它解决了:android:theme =“@ style / Theme.AppCompat .

    <application
            android:allowBackup="true"
            android:icon="@drawable/icon"
            android:label="@string/app_name_test"
            android:supportsRtl="true"
            android:theme="@style/Theme.AppCompat">
    
            ...    
    
        </application>
    
  • 6

    我在 Samsung 设备上遇到了这样的崩溃,即使该活动确实使用了Theme.AppCompat . 根本原因与三星奇怪的优化有关侧:

    - if one activity of your app has theme not inherited from Theme.AppCompat
    - and it has also `android:launchMode="singleTask"`
    - then all the activities that are launched from it will share the same Theme
    

    我的解决方案只是删除 android:launchMode="singleTask"

  • 9

    做就是了

    new AlertDialog.Builder(this)
    

    代替

    new AlertDialog.Builder(getApplicationContext())
    
  • 32

    对我来说是使用ContextThemeWrapper的解决方案:

    private FloatingActionButton getFAB() {
    Context context = new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
    FloatingActionButton fab = new FloatingActionButton(context);
    return fab;}
    

    来自Android - How to create FAB programmatically?

  • 68

    将您的主题样式父级更改为

    parent="Theme.AppCompat"
    

    这对我有用......

  • 16

    你有很多解决这个错误的方法 .

    • 您应该使用Activity或FragmentActivity而不是ActionbarActivity或AppCompatActivity

    • 如果要使用ActionbarActivity或AppCompatActivity,则应将styles.xml Theme.Holo.xxxx更改为Theme.AppCompat.Light(如有必要,请添加到DarkActionbar)

    如果您不需要有关操作栏或AppCompat的高级属性,则无需使用Actionbar或AppCompat .

  • 6

    在Android清单中,只需将活动主题更改为AppTheme,如下面的代码片段

    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:theme="@style/AppTheme">
    </activity>
    
  • 4

    这是你想在片段中使用AlertDialog的时候

    AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
                adb.setTitle("My alert Dialogue \n");
                adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
    
                      //some code
    
                } });
                adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
    
                     dialog.dismiss();
    
                    } });
                adb.show();
    
  • 1012

    我也有这个问题以及我做了什么来修复它,并且仍然使用Holo主题是采取以下步骤:

    首先我替换了这个导入:

    import android.support.v7.app.AppCompatActivity;
    

    这一个:

    import android.app.Activity;
    

    然后改变我的扩展名:

    public class MyClass extends AppCompatActivity {//...
    

    对此:

    public class MyClass extends Activity {//...
    

    并且还必须更改此导入:

    import android.support.v7.app.AlertDialog;
    

    这个导入:

    import android.app.AlertDialog;
    

    然后您可以在活动级别的清单中使用主题标记:

    android:theme="@android:style/Theme.Holo.Dialog" />
    

    最后,(除非您的项目中有其他类必须使用v7 appCompat),您可以清理并重建项目,也可以在应用程序级别的gradle构建文件中删除此条目:

    compile 'com.android.support:appcompat-v7:23.2.1'
    

    如果您的项目中有其他类必须使用v7 appCompat,那么只需清理并重建项目即可 .

  • 13

    您之所以这样,是因为您希望将以前的sdk版本中的主题样式中的Material Design应用到21. ActionBarActivity 需要 AppTheme 所以如果您还想阻止自己对AppTheme进行自定义,则只需更改styles.xml (之前的sdk 21)所以这样,可以继承App Compat主题 .

    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    

    为了这:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    
  • 3

    这个对我有用:

    <application
               android:allowBackup="true"
               android:icon="@mipmap/ic_launcher"
               android:label="@string/app_name"
               android:theme="@style/AppTheme" >
               <activity
                   android:name=".MainActivity"
                   android:label="@string/app_name"
                   android:theme="@style/Theme.AppCompat.NoActionBar">
    
                   <intent-filter>
                       <action android:name="android.intent.action.MAIN" />
    
                       <category android:name="android.intent.category.LAUNCHER" />
                   </intent-filter>
               </activity>
    </application>
    
  • 6

    您的活动正在扩展 ActionBarActivity ,这需要应用 AppCompat.theme . 从 ActionBarActivity 更改为 ActivityFragmentActivity ,它将解决问题 .

    如果您不使用操作栏,则:

    android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
    
  • 2

    快速解决方案

    在styles.xml中更改基本主题父级

    替换

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    

    <style name="AppTheme" parent="Theme.AppCompat">
    
  • 6

    根据我的经验,问题是我展示对话框的背景 . 在按钮内单击我以这种方式实例化AlertDialog:

    builder = new AlertDialog.Builder(getApplicationContext());
    

    但上下文不正确并导致错误 . 我以这种方式使用应用程序上下文更改了它:

    在声明部分:

    Context mContext;
    

    在onCreate方法中

    mContext = this;
    

    最后在我需要AlertDialog的代码中:

    start_stop = (Button) findViewById(R.id.start_stop);
    start_stop.setOnClickListener( new View.OnClickListener()
         {
                    @Override
                    public void onClick(View v)
                    {
                        if (!is_running)
                        {
                            builder = new AlertDialog.Builder(mContext);
                            builder.setMessage("MYTEXT")
                                    .setCancelable(false)
                                    .setPositiveButton("SI", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                        Task_Started = false;
                                        startTask();
                                        }
                                    })
                                    .setNegativeButton("NO",
                                            new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            dialog.cancel();
                                        }
                                    });
                            AlertDialog alert = builder.create();
                            alert.show();
                        }
                }
            }
    

    这是我的解决方案 .

  • 3

    这个解决方案对我有用 .

    设计库依赖于Support v4和AppCompat支持库,因此不要在gradle中为appcompat和设计库使用不同的版本 .

    使用

    compile 'com.android.support:appcompat-v7:23.0.0'
     compile 'com.android.support:design:23.0.0'
    

    代替

    compile 'com.android.support:appcompat-v7:23.0.0'
     compile 'com.android.support:design:23.1.0'
    
  • 12

    我遇到了同样的问题 . 因为我正在创建自定义导航抽屉 . 但我忘记在我的清单中提到这样的主题

    机器人:主题= “@风格/ Theme.AppCompat.NoActionBar”

    一旦我将上述主题添加到我的清单中,它就解决了问题 .

  • 4

    对我来说,即使我将 Theme.AppCompat 作为我的应用程序的基本主题,上述答案都没有奏效 . 我正在使用 com.android.support:design:24.1.0 所以我只是将版本更改为 24.1.1 . 在gradle sync之后,它再次工作,异常消失了 . 在我看来,问题在于一些旧版本的设计支持库 .

    只是把它放在这里以防其他答案对某些人不起作用 .

  • 6

    在我的例子中,我用ApplicationContext来扩展视图 . 当您使用ApplicationContext时,未应用主题/样式,因此虽然我的样式中有Theme.Appcompat,但它未应用并导致此异常 . 更多细节:Theme/Style is not applied when inflater used with ApplicationContext

  • 86

    我对SectionsPagerAdapter和ViewPager&Fragment的活动

    public class MyActivity extends AppCompatActivity implements ActionBar.TabListener
    ...
    ...
         @Override
            public void onPostResume(){
                super.onPostResume();
                try {
                   getSupportActionBar().setDisplayShowTitleEnabled(false);
                }catch (NullPointerException ignored){
                }
            }
    

相关问题