解决Android: You need to use a Theme.AppCompat theme (or descendant) with this activity.

今天继续读郭少的<<第一行代码>>,在研究2.4.4体验活动的生命周期,根据书上代码重新敲了一遍,在点击Start DialoyActivity按钮时,程序后台报错,如下:

/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.activityliftcycletest, PID: 4503
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activityliftcycletest/com.example.activityliftcycletest.DialogActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
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:355)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:324)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:285)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.activityliftcycletest.DialogActivity.onCreate(DialogActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.

我在AndroidManifest.xml中写的是:

<activity android:name=".DialogActivity"
android:theme="@android:style/Theme.Dialog">
</activity>

根据提示,告诉我下面这句话有问题

android:theme="@android:style/Theme.Dialog"

解决方案有2:

1、将DialogActivity继承类由AppCompatActivity改为Activity

2、将android:theme="@android:style/Theme.Dialog"改为
android:theme="@style/Theme.AppCompat.Dialog"

扫描二维码关注公众号,回复: 232088 查看本文章
 

猜你喜欢

转载自zhangshufei8001.iteye.com/blog/2377218
今日推荐