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


Recently I ran into java.lang.IllegalStateException with an error message saying You need to use a Theme.AppCompat theme (or descendant) with this activity.

Error Stacktrace:

11-17 11:37:59.239: E/AndroidRuntime(7852): FATAL EXCEPTION: main
11-17 11:37:59.239: E/AndroidRuntime(7852): java.lang.RuntimeException:
Unable to start activity ComponentInfo
{ com.code2care.example.abc/com.code2care.example.abc.Details }:
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.
run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to
use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.
onCreate(ActionBarActivityDelegate.java:147)
at android.support.v7.app.ActionBarActivityDelegateBase.
onCreate(ActionBarActivityDelegateBase.java:139)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.code2care.example.abc.Details.onCreate(Details.java:24)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more

I have an Activity A which on a button click displays an Activity B on dialog, and the theme I have applied for Activity B is android:theme = "@android:style/Theme.Dialog".

The reason for this error is that the Activity B java file extends class ActionBarActivity which requires themes from AppCompat whereas we have used a theme from Theme.Dialog

Solution

Just change the Activity that is displayed as a Dialog extends class to Activity instead of ActionBarActivity




Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.