perhaps?
2014-02-16 17:34
采纳率: 100%
浏览 1.1k

您需要在这个活动中使用 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:

 <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>

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?

转载于:https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

29条回答 默认 最新

  • derek5. 2014-02-16 17:48
    已采纳

    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.

    已采纳该答案
    打赏 评论
  • 北城已荒凉 2014-02-16 17:36

    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.

    Use

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

    Or if you dont want action bar at the top

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

    More info @

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

    Edit:

    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

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

    打赏 评论
  • hurriedly% 2014-09-04 10:44

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

    打赏 评论
  • python小菜 2014-11-27 10:11

    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">
    
    打赏 评论
  • csdnceshi62 2014-12-03 10:45

    go to your styles and put the parent

    parent="Theme.AppCompat"
    

    instead of

    parent="@android:style/Theme.Holo.Light"
    
    打赏 评论
  • 谁还没个明天 2015-01-14 21:37

    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:

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

    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.

    打赏 评论
  • 叼花硬汉 2015-02-01 18:32

    Change your theme style parent to

     parent="Theme.AppCompat"
    

    This worked for me ...

    打赏 评论
  • ℡Wang Yan 2015-04-18 06:55

    This one worked for me:

    <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>
    
    打赏 评论
  • ℙℕℤℝ 2015-04-20 20:03

    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 :

    android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 
    
    打赏 评论
  • 胖鸭 2015-04-29 00:08

    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"

    打赏 评论
  • bug^君 2015-05-03 22:10

    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.

    打赏 评论
  • ℡Wang Yan 2015-06-15 13:50

    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.

    打赏 评论
  • 乱世@小熊 2015-09-03 10:43

    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())
    

    to:

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

    And no more problems.

    打赏 评论
  • 程序go 2015-10-21 09:47

    My Activity with SectionsPagerAdapter and ViewPager & Fragment

    public class MyActivity extends AppCompatActivity implements ActionBar.TabListener
    ...
    ...
         @Override
            public void onPostResume(){
                super.onPostResume();
                try {
                   getSupportActionBar().setDisplayShowTitleEnabled(false);
                }catch (NullPointerException ignored){
                }
            }
    
    打赏 评论
  • lrony* 2016-02-09 06:09

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

    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:theme="@style/AppTheme">
    </activity>
    
    打赏 评论
  • csdnceshi62 2016-03-08 15:30

    I had the same problem, but it solved when i put this on manifest: 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>
    
    打赏 评论
  • 旧行李 2016-04-29 05:10

    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!

    打赏 评论
  • lrony* 2016-06-23 09:04

    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>
    </style>
    
    打赏 评论
  • ?yb? 2016-08-03 11:58

    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.

    use

     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'
    
    打赏 评论
  • 三生石@ 2016-08-08 00:09

    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.

    打赏 评论
  • 笑故挽风 2016-09-21 07:54

    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?

    打赏 评论
  • ℙℕℤℝ 2016-10-01 04:26

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

    <activity
                android:name="HomeActivity"
                android:screenOrientation="landscape"
                android:theme="@style/Theme.AppCompat.Light"
                android:windowSoftInputMode="stateHidden" />
    
    打赏 评论
  • 妄徒之命 2016-10-21 20:27

    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);
    
    打赏 评论
  • 游.程 2017-03-20 06:21

    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) {
    
                     dialog.dismiss();
    
                    } });
                adb.show();
    
    打赏 评论
  • hurriedly% 2017-10-22 12:57

    Quick solution.

    Change your base theme parent in styles.xml

    Replace from

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

    to

    <style name="AppTheme" parent="Theme.AppCompat">
    
    打赏 评论
  • 程序go 2017-11-18 07:34

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

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

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

    打赏 评论
  • 笑故挽风 2017-12-22 16:48

    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

    打赏 评论
  • 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()
         {
                    @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();
                        }
                }
            }
    

    This is the solution for me.

    打赏 评论
  • 喵-见缝插针 2018-06-27 10:37

    Just Do

    new AlertDialog.Builder(this)
    

    Instead of

    new AlertDialog.Builder(getApplicationContext())
    
    打赏 评论

相关推荐 更多相似问题