E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.katherine.du.everydaystudy, PID: 8337
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.katherine.du.everydaystudy/com.katherine.du.everydaystudy.up20170807.RecyclerViewDemoActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2688)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2749)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1498)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:6202)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.katherine.du.everydaystudy.up20170807.RecyclerViewDemoActivity.onCreate(RecyclerViewDemoActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6736)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2749)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1498)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:6202)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
重点在于:You need to use a Theme.AppCompat theme (or descendant) with this activity.
即RecyclerViewDemoActivity需要Theme.AppCompat的主题。
原因:RecyclerViewDemoActivity继承了AppCompatActivity
解决方法:
第一步:在styles.xml文件中自定义MyAppCompactTheme,其parent只要是Theme.AppCompat的主题就行
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<style name="MyAppCompactTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" type="text/css">
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
第二步:在AndroidManifest.xml文件中,将RecyclerViewDemoActivity的主题设置为MyAppCompactTheme即可
<activity
android:name=".up20170807.RecyclerViewDemoActivity"
android:theme="@style/MyAppCompactTheme">
</activity>