2014-02-16 17:34

您需要在这个活动中使用 Theme.AppCompat 主题(或子主题)

Android Studio 0.4.5

Android documentation for creating custom dialog boxes: http://developer.android.com/guide/topics/ui/dialogs.html

If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in the <activity> manifest element:

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

However, when I tried this I get the following exception:

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

I am supporting the following, and I can't using something greater than 10 for the min:

minSdkVersion 10
targetSdkVersion 19

In my styles I have the following:

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

And in my manifest I have this for the activity:

        android:theme="@style/AppTheme" >
            android:label="@string/title_activity_dialog_update" >

Creating the dialog box like this was something I was hopping to do, as I have already completed the layout.

Can anyone tell me how I can get around this problem?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • csdnceshi54 hurriedly% 7年前

    The reason you are having this problem is because the activity you are trying to apply the dialog theme to is extending ActionBarActivity which requires the AppCompat theme to be applied.

    Update: Extending AppCompatActivity would also have this problem

    In this case, change the Java inheritance from ActionBarActivity to Activity and leave the dialog theme in the manifest as it is, a non Theme.AppCompat value

    The general rule is that if you want your code to support older versions of Android, it should have the AppCompat theme and the java code should extend AppCompatActivity. If you have *an activity that doesn't need this support, such as you only care about the latest versions and features of Android, you can apply any theme to it but the java code must extend plain old Activity.

    NOTE: When change from AppCompatActivity (or a subclass, ActionBarActivity), to Activity, must also change the various calls with "support" to the corresponding call without "support". So, instead of getSupportFragmentManager, call getFragmentManager.

    点赞 27 评论 复制链接分享
  • csdnceshi73 喵-见缝插针 6年前

    Copying answer from @MarkKeen in the comments above as I had the same problem.

    I had the error stated at the top of the post and happened after I added an alert dialog. I have all the relevant style information in the manifest. My problem was cured by changing a context reference in the alert builder - I changed:

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


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

    And no more problems.

    点赞 29 评论 复制链接分享
  • csdnceshi74 7*4 7年前

    All you need to do is add android:theme="@style/Theme.AppCompat.Light" to your application tag in the AndroidManifest.xml file.

    点赞 25 评论 复制链接分享
  • csdnceshi69 YaoRaoLov 5年前

    For me none of the above answers worked even after I had Theme.AppCompat as my base theme for the application. I was using com.android.support:design:24.1.0 So I just changed the version to 24.1.1. After the gradle sync, Its working again and the exception went away. Seems to me the issue was with some older versions of design support libraries.

    Just putting this here in case other answers don't work for some people.

    点赞 10 评论 复制链接分享
  • weixin_41568131 10.24 4年前

    This is when you want a AlertDialog in a Fragment

                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) {
                    } });
    点赞 10 评论 复制链接分享
  • weixin_41568196 撒拉嘿哟木头 5年前

    I had the same problem, but it solved when i put this on manifest: android:theme="@style/Theme.AppCompat.

    点赞 9 评论 复制链接分享
  • csdnceshi67 bug^君 5年前

    for me was solution to use ContextThemeWrapper:

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

    from Android - How to create FAB programmatically?

    点赞 9 评论 复制链接分享
  • csdnceshi61 derek5. 6年前

    I had such crash on Samsung devices even though the activity did use Theme.AppCompat. The root cause was related to weird optimizations on Samsung side:

    - 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

    My solution was just removing android:launchMode="singleTask"

    点赞 9 评论 复制链接分享
  • csdnceshi59 ℙℕℤℝ 6年前

    This is what fixed it for me: instead of specifying the theme in manifest, I defined it in onCreate for each activity that extends ActionBarActivity:

    protected void onCreate(Bundle savedInstanceState) {

    Here MyAppTheme is a descendant of Theme.AppCompat, and is defined in xml. Note that the theme must be set before super.onCreate and setContentView.

    点赞 9 评论 复制链接分享
  • csdnceshi62 csdnceshi62 6年前

    Change your theme style parent to


    This worked for me ...

    点赞 8 评论 复制链接分享
  • csdnceshi54 hurriedly% 3年前

    In my experiences the problem was the context where I showed my dialog. Inside a button click I instantiate an AlertDialog in this way:

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

    But the context was not correct and caused the error. I've changed it using the application context in this way:

    In declare section:

    Context mContext;

    in the onCreate method

    mContext = this;

    And finally in the code where I need the AlertDialog:

    start_stop = (Button) findViewById(R.id.start_stop);
    start_stop.setOnClickListener( new View.OnClickListener()
                    public void onClick(View v)
                        if (!is_running)
                            builder = new AlertDialog.Builder(mContext);
                                    .setPositiveButton("SI", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                        Task_Started = false;
                                            new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                            AlertDialog alert = builder.create();

    This is the solution for me.

    点赞 8 评论 复制链接分享
  • csdnceshi62 csdnceshi62 3年前

    Just Do

    new AlertDialog.Builder(this)

    Instead of

    new AlertDialog.Builder(getApplicationContext())
    点赞 8 评论 复制链接分享
  • csdnceshi73 喵-见缝插针 7年前

    go to your styles and put the parent


    instead of

    点赞 8 评论 复制链接分享
  • csdnceshi60 ℡Wang Yan 6年前

    Your Activity is extending ActionBarActivity which requires the AppCompat.theme to be applied. Change from ActionBarActivity to Activity or FragmentActivity, it will solve the problem.

    If you use no Action bar then :

    点赞 8 评论 复制链接分享
  • csdnceshi53 Lotus@ 5年前

    I was experiencing this problem even though my Theme was an AppCompat Theme and my Activity was an AppCompatActivity (or Activity, as suggested on other's answers). So I cleaned, rebuild and rerun the project.

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

    It may seem dumb, but now it works great!

    Just hope it helps!

    点赞 7 评论 复制链接分享
  • csdnceshi55 ~Onlooker 4年前

    I had this problem as well and what I did to fix it, AND still use the Holo theme was to take these steps:

    first I replaced this import:

    import android.support.v7.app.AppCompatActivity;

    with this one:

    import android.app.Activity;

    then changed my extension from:

    public class MyClass extends AppCompatActivity {//...

    to this:

    public class MyClass extends Activity {//...

    And also had to change this import:

    import android.support.v7.app.AlertDialog;

    to this import:

    import android.app.AlertDialog;

    and then you can use your theme tag in the manifest at the activity level:

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

    and lastly, (unless you have other classes in your project that has to use v7 appCompat) you can either clean and rebuild your project or delete this entry in the gradle build file at the app level:

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

    if you have other classes in your project that has to use v7 appCompat then just clean and rebuild the project.

    点赞 7 评论 复制链接分享
  • weixin_41568131 10.24 5年前

    Change the theme of the desired Activity. This works for me:

                android:windowSoftInputMode="stateHidden" />
    点赞 6 评论 复制链接分享
  • csdnceshi65 larry*wei 5年前

    In my case i have no values-v21 file in my res directory. Then i created it and added in it following codes:

      <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>
    点赞 6 评论 复制链接分享
  • csdnceshi68 local-host 6年前

    If you need to extend ActionBarActivity you need on your style.xml:

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

    If you set as main theme of your application as android:Theme.Material.Light instead of AppTheme.Base then you’ll get an “IllegalStateException:You need to use a Theme.AppCompat theme (or descendant) with this activity” error.

    点赞 6 评论 复制链接分享
  • csdnceshi77 狐狸.fox 6年前

    You have many solutions to that error.

    1. You should use Activity or FragmentActivity instead of ActionbarActivity or AppCompatActivity

    2. If you want use ActionbarActivity or AppCompatActivity, you should change in styles.xml Theme.Holo.xxxx to Theme.AppCompat.Light (if necessary add to DarkActionbar)

    If you don't need advanced attributes about action bar or AppCompat you don't need to use Actionbar or AppCompat.

    点赞 6 评论 复制链接分享
  • weixin_41568127 ?yb? 7年前

    You have came to this because you want to apply Material Design in your theme style in previous sdk versions to 21. ActionBarActivity requires AppThemeso if you also want to prevent your own customization about your AppTheme, only you have to change in your styles.xml (previous to sdk 21) so this way, can inherit for an App Compat theme.

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

    for this:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    点赞 6 评论 复制链接分享
  • weixin_41568134 MAO-EYE 4年前

    In my case, i was inflating a view with ApplicationContext. When you use ApplicationContext, theme/style is not applied, so although there was Theme.Appcompat in my style, it was not applied and caused this exception. More details: Theme/Style is not applied when inflater used with ApplicationContext

    点赞 6 评论 复制链接分享
  • csdnceshi52 妄徒之命 6年前

    My Activity with SectionsPagerAdapter and ViewPager & Fragment

    public class MyActivity extends AppCompatActivity implements ActionBar.TabListener
            public void onPostResume(){
                try {
                }catch (NullPointerException ignored){
    点赞 5 评论 复制链接分享
  • csdnceshi74 7*4 5年前

    This solution worked for me.

    Design library depends on the Support v4 and AppCompat Support Libraries, so don't use different version for appcompat and design library in gradle.


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

    instead of

     compile 'com.android.support:appcompat-v7:23.0.0'
     compile 'com.android.support:design:23.1.0'
    点赞 5 评论 复制链接分享
  • csdnceshi70 笑故挽风 4年前

    I was getting this same problem. Because i was creating custom navigation drawer. But i forget to mention theme in my manifest like this


    As soon i added the above the theme to my manifest it resolved the problem.

    点赞 4 评论 复制链接分享
  • weixin_41568208 北城已荒凉 4年前

    Quick solution.

    Change your base theme parent in styles.xml

    Replace from

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


    <style name="AppTheme" parent="Theme.AppCompat">
    点赞 3 评论 复制链接分享
  • csdnceshi68 local-host 7年前

    min sdk is 10. ActionBar is available from api level 11. So for 10 you would be using AppCompat from the support library for which you need to use Theme.AppCompat or descendant of the same.


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

    Or if you dont want action bar at the top


    More info @



    I might have misread op post.

    Seems op wants a Dialog with a Activity Theme. So as already suggested by Bobbake4 extend Activity instead of ActionBarActivity.

    Also have a look @ Dialog Attributes in the link below


    点赞 2 评论 复制链接分享
  • weixin_41568127 ?yb? 5年前

    This worked for me, I just changed:

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

    to this:

    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    点赞 2 评论 复制链接分享
  • weixin_41568183 零零乙 6年前

    This one worked for me:

               android:theme="@style/AppTheme" >
                       <action android:name="android.intent.action.MAIN" />
                       <category android:name="android.intent.category.LAUNCHER" />
    点赞 2 评论 复制链接分享
  • csdnceshi77 狐狸.fox 5年前

    In Android manifest just change theme of activity to AppTheme as follow code snippet

    点赞 1 评论 复制链接分享