Caused By: Java.lang.illegalstateexception: You Need To Use A Theme-appcompat

/ Comments off
  1. Caused By Java.lang.illegalstateexception You Need To Use A Theme.appcompat Theme

Join GitHub today

GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.

Sign up New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

背景: 集成环信项目,某些个别界面,打开或者启动alterDialog就会崩机,报如图说下错误 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity 原因: 1 该Acitivity继承了某些兼容Activit. It should recreate the manifest using the newer version. Then you enable all the require permissions for your sketch, if any. Not tested, but I believe I read it in the documentation not long ago.

Comments

commented Jun 30, 2017

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at com.didi.virtualapk.demo.aidl.BookManagerActivity.onCreate(BookManagerActivity.java:95)
at android.app.Activity.performCreate(Activity.java:6302)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at com.didi.virtualapk.internal.VAInstrumentation.callActivityOnCreate(VAInstrumentation.java:141)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2383)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5456)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)

commented Jun 30, 2017

@MissBears 插件的构建请参考:Demo 工程运行须知

commented Jun 30, 2017

可能你的版本比较低吧!

commented Jun 30, 2017

我也是类似的问题

commented Jun 30, 2017

我也是类似的情况

commented Jun 30, 2017

360马上也要开源rePlugin 了,看看哪个好用就用哪个吧

commented Jun 30, 2017

plugindemo的BookManagerActivity是继承的AppCompatActivity,但是在manifest里面没有使用v7包的主题,把BookManagerActivity改成直接继承Activity就好了

Caused By Java.lang.illegalstateexception You Need To Use A Theme.appcompat Theme

commented Jun 30, 2017

我连主题都删了,都不行了啊,我用FragmentActivity的啊

commented Jun 30, 2017

@moziqi @xns1001@tokiii@MissBears 插件需要执行 gradle assemblePlugin任务来进行插件处理并生成apk, 不能用常规方式生成的apk让宿主去加载。

referenced this issue Jul 27, 2017

Closed

Unknown element under <manifest>: #89

referenced this issue Mar 16, 2018

Closed

JNI ERROR (app bug) local reference table overflow max=512 #192

referenced this issue Nov 26, 2018

Open

JNI ERROR (app bug): local reference table overflow (max=512) #279

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

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:

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

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

In my styles I have the following:

And in my manifest I have this for the 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?

Zoe
16.1k9 gold badges62 silver badges94 bronze badges
ant2009ant2009
190121 gold badges329 silver badges514 bronze badges

44 Answers

12 next

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.

Bobbake4Bobbake4
19.9k8 gold badges48 silver badges83 bronze badges

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

Michele La Ferla
5,03310 gold badges34 silver badges62 bronze badges
iustingiusting
5,9551 gold badge10 silver badges26 bronze badges

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:

to:

And no more problems.

Pang
7,10216 gold badges68 silver badges107 bronze badges
A.K.A.K.
2,6561 gold badge13 silver badges17 bronze badges

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

Or if you dont want action bar at the top

More info @

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

shareef
5,4018 gold badges45 silver badges72 bronze badges
RaghunandanRaghunandan
120k19 gold badges198 silver badges237 bronze badges
DarushDarush
5,2813 gold badges34 silver badges44 bronze badges

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!

Geraldo NetoGeraldo Neto

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:

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.

k29k29
1,5601 gold badge12 silver badges13 bronze badges
Yamen NassifYamen Nassif
1,5461 gold badge12 silver badges32 bronze badges

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

Ali GürelliAli Gürelli
1,2441 gold badge15 silver badges21 bronze badges

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

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.

JonasOliveiraJonasOliveira

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

sharma_kunalsharma_kunal

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:

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

goRGongoRGon

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

Marc BaduqMarc Baduq
Ali AkramAli Akram
8851 gold badge9 silver badges18 bronze badges

for me was solution to use ContextThemeWrapper:

from Android - How to create FAB programmatically?

Community
PeterPeter

In my case such issue was appear when i tried to show Dialog.The problem was in context, I've use getBaseContext() which theoretically should return Activity context, but appears its not, or it return context before any Theme applied.

So I just replaced getBaseContexts() with 'this', and now it work as expected.

Vitaliy AVitaliy A
2,2271 gold badge22 silver badges27 bronze badges

I had an activity with theme <android:theme='@android:style/Theme.Dialog'> used for showing dialog in my appWidget and i had same problem

i solved this error by changing activity code like below:

Elyas NateghElyas Nategh

Change your theme style parent to

This worked for me ...

FazalFazal
2,2991 gold badge10 silver badges20 bronze badges

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

KalogluKaloglu
8601 gold badge12 silver badges26 bronze badges

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

maxhb
6,2417 gold badges22 silver badges44 bronze badges
shikhashikha

This is when you want a AlertDialog in a Fragment

Avinash_ksAvinash_ks

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:

with this one:

then changed my extension from:

to this:

And also had to change this import:

to this import:

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

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:

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

WraithiousWraithious

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.

for this:

danigonlineadanigonlineaCaused By: Java.lang.illegalstateexception: You Need To Use A Theme-appcompat
7181 gold badge9 silver badges16 bronze badges
user1501382user1501382

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 :

Md Imran ChoudhuryMd Imran Choudhury
3,7171 gold badge28 silver badges38 bronze badges

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

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

In declare section:

in the onCreate method

And finally in the code where I need the AlertDialog:

This is the solution for me.

DanteDante
Caused by java.lang.illegalstateexception you need to use a theme.appcompat theme

Quick solution.

Change your base theme parent in styles.xml

Replace from

to

KalogluKaloglu
8601 gold badge12 silver badges26 bronze badges

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

instead of

kiran boghrakiran boghra
2,2521 gold badge14 silver badges21 bronze badges

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

Need

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

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

Azhar oswsAzhar osws

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.

Sharp EdgeSharp Edge
3,2462 gold badges19 silver badges33 bronze badges
12 next

protected by CommunityApr 2 '16 at 21:18

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged androidandroid-layout or ask your own question.