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

原创 2016年06月02日 00:53:20
需要定义一个半透明的activity,在manifest中添加 android:theme="@android:style/Theme.Translucent" 
结果报错"You need to use a Theme.AppCompat theme (or descendant) with this activity." 由于我的主题设置为
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">,猜想是不是这个原因造成的(没有去验证),网上的很多方法是设置直接
继承 Activity。
我查了下Theme.Translucent的源码,为:
<style name="Theme.Translucent">
    <item name="windowBackground">@color/transparent</item>
    <item name="colorBackgroundCacheHint">@null</item>
    <item name="windowIsTranslucent">true</item>
    <!-- Note that we use the base animation style here (that is no
         animations) because we really have no idea how this kind of
         activity will be used. -->
    <item name="windowAnimationStyle">@style/Animation</item>
</style>
根据源码我在style.xml中自定了<style name="Theme.AppCompat.Translucent">,将源码中的item复制过去:
<style name="Theme.AppCompat.Translucent">
    <item name="windowBackground">@color/transparent</item>
    <item name="colorBackgroundCacheHint">@null</item>
    <item name="windowIsTranslucent">true</item>
    <item name="windowAnimationStyle">@style/Animation</item>
</style>
结果报错:No resource found that matches the given name: attr 'colorBackgroundCacheHint'.
          No resource found that matches the given name: attr 'windowAnimationStyle'.
          No resource found that matches the given name: attr 'windowBackground'.
          No resource found that matches the given name: attr 'windowIsTranslucent'.
后来发现复制的<item name="windowBackground">@color/transparent</item>如果用快捷键去关联生成的是:
<item name="android:windowBackground">@android:color/transparent</item>,最后修改为:
<style name="Theme.AppCompat.Translucent">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
程序运行通过。通过这个说明其他的定义主题出现的类似问题,都可以这样解决。
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

you need to use a theme.appcompat theme (or descendant) with this activity 解决办法

当你想隐藏 Androidmanifest.xml android:n

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

当我的MainActivity继承自v7包中的ActionBarActivity或者AppCompatActivity时,如果在style.xml文件中指定MainActivity所使用的样式如下: ...

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

错误描述为: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) wit...

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

错误描述为: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) wit...

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

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

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

错误描述为: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) wit...

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

搜索错误如下: You need to use a Theme.AppCompat theme (or descendant) with this activity java.lang.Run...

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

错误描述为: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) wit...

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

在项目中遇到了这样一个问题,今天解决了,记录下来并且与大家共勉。。。 我先说我的需求,, 我要将欢迎也设置成全屏,并且没有标题栏、和状态栏,,然后我就出现了这个问题: java.lang.Ill...

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

微信支付它给的WXPayEntryActivity主题改错了java.lang.RuntimeException: Unable to start activity ComponentInfo{包名/...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)