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

csdnceshi55
~Onlooker I think the only problem in this case is the context you provide for Dialog constractor. See my answer. stackoverflow.com/a/51574281/232727
2 年多之前 回复
weixin_41568196
撒拉嘿哟木头 I have come up with same error and solved this by stackoverflow.com/questions/29797134/…
接近 3 年之前 回复
csdnceshi62
csdnceshi62 Doing this will remove the error but not get him to where he wants to be which is an activity with a dialog theme. The general rule is that if you want your activity to have an action bar it should have the AppCompat theme and the java code should extend ActionBarActivity. If you have an activity that doesn't need an action bar (like a dialog themed activity) you can apply any theme to it but the java code must extend plain old activity.
6 年多之前 回复
csdnceshi78
程序go you want a full screen dialog?
6 年多之前 回复
weixin_41568174
from.. said: in your AndroidManifest.xml, the theme you are specifying for your activity is overriding the theme you are specifying for your application. Remove the android:theme line from the <activity> tag.
6 年多之前 回复
csdnceshi78
程序go You are using AppCompat which is form the support library to support actionbars below api level 11. Just use android:theme="@style/AppTheme" > for the activity in manifest
6 年多之前 回复
csdnceshi57
perhaps? I am new to this but looking at my styles I have the following: <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> And I have the following imports: import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; But not sure what all this means.
6 年多之前 回复
csdnceshi78
程序go Are you using AppCompat from the support library?
6 年多之前 回复

29个回答

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.

csdnceshi75
衫裤跑路 Change ActionBarActivity is not a solution is a wrong workarround, because it affects the architecture of the Application. The solution is, as mentioned in other threads, to change contest for activity.this.
2 年多之前 回复
weixin_41568134
MAO-EYE - when you change from AppCompatActivity (or a subclass, ActionBarActivity), to Activity, you must change the various calls with "support" to the corresponding call without "support". So, instead of getSupportFragmentManager, call getFragmentManager.
3 年多之前 回复
csdnceshi58
Didn"t forge Nice one........
大约 4 年之前 回复
csdnceshi64
游.程 If you change from ActionBarActivity to Activity, operation like getSupportFragmentManager() will not be able. So it's not the right answer.
大约 4 年之前 回复
csdnceshi57
perhaps? for his activity. If you want to do this your activity cannot extend AppCompatActivity. If you are getting this error and do not have a requirement of using that theme then you can update the theme to solve the issue, but I do believe this answer is correct for the original posted question.
4 年多之前 回复
csdnceshi60
℡Wang Yan This answer is not correct if you want to implement Chromecast and Mediarouter. See tutorial for this. You then need AppCompatActivity which need its corresponding theme.
4 年多之前 回复
csdnceshi78
程序go Thanks. @Bobbake4
接近 5 年之前 回复
weixin_41568127
?yb? stackoverflow.com/a/21492572/150371 might be a good solution to overcome this as well
大约 5 年之前 回复
csdnceshi70
笑故挽风 Thank you so much, you saved me time as from exceptions it was not clear where the problem is. I tried to changed android:theme in androidmanifest.xml then also in styles.xml and also in both but it still threw exception from which it was not clear how to make it working :)
5 年多之前 回复
weixin_41568131
10.24 So why android studio generates MainActivity.java extending ActionBarActivity as default?
5 年多之前 回复
csdnceshi63
elliott.david In style <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> this solved my issue. :)
接近 6 年之前 回复
csdnceshi51
旧行李 thanks, for respond. I afraid that removing ActionBarActivity is not an option. But I have to show the activity as a Dialog. Do you have any idea how to do that? If I found any workaround how to use Theme.AppCompat I will let you know.
接近 6 年之前 回复
csdnceshi57
perhaps? If your Java is using ActionBarActivity the theme must extend Theme.AppCompat. There are a handful of built in themes that do extend Theme.AppCompat or you could extend it yourself and modify it. Without removing the ActionBarActivity parent you cannot get around using the Theme.AppCompat.
接近 6 年之前 回复
csdnceshi51
旧行李 Im having the same problem, and this solution is impossible to work for me. The thing is that I can't extend Activity, because I have a bunch of classes (whole project) that relay on ActionBarActivity. Is there any other solution to the problem? Thx
接近 6 年之前 回复
csdnceshi53
Lotus@ Thanks @Bobbake4 so maybe we should report it.
大约 6 年之前 回复
csdnceshi57
perhaps? If you're talking about when you create a new project in Android Studio I'm guessing that's a bug in the standard config.
大约 6 年之前 回复
csdnceshi53
Lotus@ Omg @Bobbake4! You're the real MVP! A question, I just created the app and this error was on the code, is that a bug? Or I'm missing something
大约 6 年之前 回复
weixin_41568196
撒拉嘿哟木头 Thanks @Bobbake4, that was my understanding also. The app compiles fine in IntelliJ and ant, and it's during my trying to mavenize it that I get the error. Probably something to do with the apklib dependency on the appcompat library....I'll need to investigate more myself first I think.
6 年多之前 回复
csdnceshi57
perhaps? Nope shouldn't have to. The docs for the application theme say this will be the default theme for all activities (developer.android.com/guide/topics/manifest/…). If you want to create a new question with your manifest posted it might be easier to help diagnose. Link your question here.
6 年多之前 回复
weixin_41568196
撒拉嘿哟木头 I'm getting this error, even when I have the Theme applied to my App thus: <application android:theme="@style/Theme.AppCompat.Light"> do I have to apply to all Activities individually as well?
6 年多之前 回复
csdnceshi57
perhaps? I'm not sure what you are asking here. Are you asking if there is a way to have an Activity inherit from ActionBarActivity without setting the theme to an AppCompat theme? If that is what you are asking no, you cannot extend ActionBarActivity without applying one of the AppCompat themes or extending one of those.
6 年多之前 回复
csdnceshi65
larry*wei is there any other workaround without changing the parent ActionBarActivity?
6 年多之前 回复
csdnceshi55
~Onlooker thanks you was correct. I have changed the code as you specified. And now it is working.
6 年多之前 回复
csdnceshi57
perhaps? He's trying to create a dialog themed activity which would not have an action bar. You can easily do this when using the AppCompat library but you cannot if your activity extends ActionBarActivity. If he changes the java code to extend from Activity he will have a dialog themed activity like his question is requesting. @ant2009 shouldn't you be changing com.ssd.register.Dialog_update to extend from Activity not MainActivity?
6 年多之前 回复
csdnceshi55
~Onlooker So I have now changed my class to extend activity i.e. public class MainActivity extends Activity {. However, I am still getting the same problem.
6 年多之前 回复
csdnceshi77
狐狸.fox op is using appcompat in which case he should extend ActionBarActivity which is what is shown from his import statements from the comment below the quesiton.
6 年多之前 回复

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

csdnceshi75
衫裤跑路 this should be accepted answer - to help new comers(like me :P) coming here
接近 4 年之前 回复
csdnceshi58
Didn"t forge Thanks man, working perfect
大约 4 年之前 回复
csdnceshi61
derek5. you saved my day sir. this should be the answer
大约 4 年之前 回复
csdnceshi73
喵-见缝插针 if you are like me, you have forget to add the same style name in general file styles.xml than you put in (V21) styles.xml file (without the properties specific to V21). If you declare a style in V21 folder and your device have a version below. It can't find it so an exception is fired.
4 年多之前 回复
csdnceshi69
YaoRaoLov Ran into this issue as well using the SnackBar. This solved my issue.
大约 5 年之前 回复
csdnceshi60
℡Wang Yan This would not allow me to change the background color of the ActionBar. It would igrnore whatever value I used. Until I changed the activity class from ActionBarActivity to Activity as mentioned in the top rated answer.
5 年多之前 回复
csdnceshi79
python小菜 This didn't really work for me because my title bars disappeared when I used anything but holo...
5 年多之前 回复
csdnceshi77
狐狸.fox This is basically deleting the reference to the Material theme. I don't think this helps getting anywhere. It's the same as saying NOT to use Material theme.
5 年多之前 回复
csdnceshi51
旧行李 This is the only answer that actually worked for me.
5 年多之前 回复
csdnceshi53
Lotus@ This is not a valid answer for Android Studio, though it in some cases it will work as a quick fix when one is bypassing styles.xml files. In Android Studio, proper way to define theme is to define them in styles.xml files, and then the Android menifest file will merely refer to them, and select them based on which Android version you are running on your device. Usually it is set to android:theme="@style/AppTheme" and this AppTheme refers to <style name="Theme.AppTheme" parent="Theme.AppCompat.Light"> in styles.xml
5 年多之前 回复
weixin_41568196
撒拉嘿哟木头 I have a theme added to each activity and application. Still does not work. There is still more to it.
接近 6 年之前 回复
csdnceshi62
csdnceshi62 In the code, ant2009 has AppTheme set as the application them which extends from a descendant of Theme.AppCompat. That should work. I had the same problem after upgrading to 5.0 on my device and ended up setting @style/Theme.AppCompat.Light as the theme on my activity. Something not right here...
接近 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())

to:

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

And no more problems.

csdnceshi73
喵-见缝插针 Thanks, this solved my issue, because I was calling this dialog from a different fragment, but the alert dialog needs to open from a floating action button which needs to be part of the main activity.
2 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 I knew completely this was the problem.. For those of you using builder in some listview(like me :P) getApplicationContext() is so tempting to use. Well this highlights with an error cannot be applied. Instead use ActivityName.this. Hope it helps for someone out there.
2 年多之前 回复
csdnceshi80
胖鸭 had the same issue, changed my style but still crashed. only worked after setting this to constructor
2 年多之前 回复
csdnceshi62
csdnceshi62 In my onCreate I had been saving a member variable to hold context using getApplicationContext(). This answer led me to instead save context as "this", and crash went away. Thanks so much.
接近 3 年之前 回复
weixin_41568110
七度&光 You are my hero! Worked for me.
接近 3 年之前 回复
csdnceshi58
Didn"t forge This solve the program for me, also you would use getContext() instead of this if you're inside an ArrayAdapter or similar adapter class.
接近 3 年之前 回复
csdnceshi60
℡Wang Yan Sure worked for me..
大约 3 年之前 回复
csdnceshi63
elliott.david thanks, it was solve my problems...
3 年多之前 回复
csdnceshi61
derek5. Did the opposite of what is suggested. And it worked :P. Thanks!
大约 4 年之前 回复
csdnceshi70
笑故挽风 Or, if you are building your AlertDialog in a fragment, rather than directly in an Activity, use getActivity() rather than this as suggested above and the problem is fixed. As for why, I can only surmise the older AlertDialog.Builder(xxx) code either wanted a different type passed in, or was simply less picky about what got passed in, while the current Builder() really, really wants an Activity. Since Google has chosen to hide (remove?) the documentation for the old code, presumably in an effort to encourage use of the v7.app version, I know of no way to tell for sure.
4 年多之前 回复
csdnceshi78
程序go It happened with me also. I was just trying and your answer really worked! What is the logic for it to get worked? I really didn't understood.
接近 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);
csdnceshi71
Memor.の Worked for me...
接近 2 年之前 回复
csdnceshi72
谁还没个明天 Thanks! This worked.
大约 2 年之前 回复
csdnceshi57
perhaps? you need an activity, not a context, to display dialogs
接近 3 年之前 回复
csdnceshi56
lrony* refer to this link: stackoverflow.com/questions/22966601/…
3 年多之前 回复
csdnceshi51
旧行李 Your solution helped me in my case. Can you explain what difference between getApplicationContext() and MainActivity.this?
3 年多之前 回复
csdnceshi78
程序go This was my case. Thank you!
3 年多之前 回复
csdnceshi79
python小菜 Same if you're using LayoutInflater.from(<aplication context>), make sure you use the activity context or the provided LayoutInflater so that it inherits the right Theme.
3 年多之前 回复

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" />
csdnceshi78
程序go This helped me.
2 年多之前 回复

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">
csdnceshi61
derek5. Follow these steps stackoverflow.com/a/46980945/4897420
2 年多之前 回复
csdnceshi72
谁还没个明天 does not help me, the exception still triggers
2 年多之前 回复

Change your theme style parent to

 parent="Theme.AppCompat"

This worked for me ...

Just Do

new AlertDialog.Builder(this)

Instead of

new AlertDialog.Builder(getApplicationContext())
weixin_41568196
撒拉嘿哟木头 It worked! But why?
大约 2 年之前 回复
csdnceshi64
游.程 Working solution! 30 minutes to find out with this answer. Why I´m using new AlertDialog.Builder(getApplicationContext())?
2 年多之前 回复

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.

csdnceshi53
Lotus@ You're correct that you shouldn't be using an Application context the way you originally had it. What you've done is use an Activity as the context instead. This is not, however, an Application context or a subclass of it. So it's not right to say you're using an Application context in a different way.
2 年多之前 回复

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>
共29条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐