原文地址:https://blog.csdn.net/abren32/article/details/44652799
java.lang.RuntimeException: Unable to start activity ComponentInfo{easycarinsurance.com.autoinsuranceandoridclient/easycarinsurance.com.autoinsuranceandoridclient.FirstActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
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:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at easycarinsurance.com.autoinsuranceandoridclient.BaseActivity.onCreate(BaseActivity.java:34)
at easycarinsurance.com.autoinsuranceandoridclient.FirstActivity.onCreate(FirstActivity.java:32)
at android.app.Activity.performCreate(Activity.java:5232)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2272)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
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:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at easycarinsurance.com.autoinsuranceandoridclient.BaseActivity.onCreate(BaseActivity.java:34)
at easycarinsurance.com.autoinsuranceandoridclient.FirstActivity.onCreate(FirstActivity.java:32)
at android.app.Activity.performCreate(Activity.java:5232)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2272)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
03-26 17:15:47.388 12953-12953/easycarinsurance.com.autoinsuranceandoridclient I/Process﹕ Sending signal. PID: 12953 SIG: 9
如上是运行后的报错信息,其中关键是一句话是:You need to use a Theme.AppCompat theme (or descendant) with this activity。
啥意思呢:你需要为这个Activity使用一个Theme.AppCompat 的主题 或者在 继承这个Theme.AppCompat主题的descendant 子主题上使用。
我们来分析下出现这样的原因:
1. 项目工程里面没有使用这样的Theme.AppCompat主题,我们可以在values/styles.xml里面加上。
2. 项目工程里values/styles.xml使用了,但是values-v11/styles.xml、values-v14/styles.xml、values-v21/styles.xml不同api平台下适配的主题里面没有使用这样的主题,或者说 没有使用一致的主题,所以要改成一致的。
3. 项目工程各个平台适配的样式都使用了一致的主题,还出现这样的错误,那就要检查运行时程序所引用的主题名字是不是我们values里面的定义的主题,还是变成build.gradle下的dependencies进来的别的组件项目里的别人的主题,因为往往如果使用的主题名字命名为:AppTheme、BaseAppTheme等常用的名称程序容易首先的引用是别的组件库的主题。所以建议在使用主题名字的时候尽量使用只属于这个项目的唯一的主题名称。
关于第三种原因,我也是百思不得其解才发现的,当我在AndroidMenifest里面 跟踪android:theme="@style/AppTheme"主题引用的时候才发现,妈蛋竟然除了项目本身对应的主题,还有第三方的项目库的也是用“AppTheme”这个名字,于是我大胆的把自己项目的所有引用该主题的地方重新命名。果然clean project-->rebuild project--->run后,终于松了一口屁,运行成功。