【我的Android进阶之旅】解决bug:You need to use a Theme.AppCompat theme (or descendant) with this activity....

前言

今天用Android Studio 生成Activity的时候,默认继承AppCompatActivity ,而在AndroidManifest.xml我对该Activity设置了一个主题,然后运行的时候报了错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xtc.watch/com.xtc.watch.view.defaultprompt.activity.DefaultPromptActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

如下所示:


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xtc.watch/com.xtc.watch.view.defaultprompt.activity.DefaultPromptActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729)
         at android.app.ActivityThread.-wrap12(ActivityThread.java)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1576)
         at android.os.Handler.dispatchMessage(Handler.java:111)
         at android.os.Looper.loop(Looper.java:207)
         at android.app.ActivityThread.main(ActivityThread.java:5905)
         at java.lang.reflect.Method.invoke(Native Method)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
      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:124)
         at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
         at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
         at com.xtc.watch.view.defaultprompt.activity.DefaultPromptActivity.onCreate(DefaultPromptActivity.java:30)
         at android.app.Activity.performCreate(Activity.java:6298)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729) 
         at android.app.ActivityThread.-wrap12(ActivityThread.java) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1576) 
         at android.os.Handler.dispatchMessage(Handler.java:111) 
         at android.os.Looper.loop(Looper.java:207) 
         at android.app.ActivityThread.main(ActivityThread.java:5905) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749) 

这里写图片描述

AndroidManifest.xml文件中关于该Activity的配置如下:

   <activity android:name=".view.defaultprompt.activity.DefaultPromptActivity"
               android:theme="@style/dialog_watch_upgrade_style">
         <intent-filter>
             <action android:name="android.intent.action.MAIN"/>
             <category android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>
     </activity>

而DefaultPromptActivity继承于AppCompatActivity ,如下代码所示:

public class DefaultPromptActivity extends AppCompatActivity {

解决方法

我这里就无意中继承了ActionBarActivity,它来自android.support.v7.app.ActionBarActivity。
所以就要使用与其配合的AppCompat的theme才行,将DefaultPromptActivity改为继承于Activity ,如下代码所示:

public class DefaultPromptActivity extends Activity {

这个bug就解决了。

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/52749732

这里写图片描述

转载于:https://www.cnblogs.com/ouyangpeng/p/8537692.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值