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

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activitylifecycletest/com.example.activitylifecycletest.DialogActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThreadH.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInitMethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:103)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:555)
        at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:466)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.activitylifecycletest.DialogActivity.onCreate(DialogActivity.java:11)
        at android.app.Activity.performCreate(Activity.java:6285)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThreadH.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:103)

以上错误代码,出现的问题一般在Android Studio可以比较容易定位解决,解决办法有二:

  1. 找到报错的activity(以上错误日志是com.example.activitylifecycletest.DialogActivity)修改AndroidManifest.xml中theme主题的,使之引用theme.appcompat.*** 就可以了 ps:如果无效,可以修改Application的theme,同样是引用于the.appcompat。
  2. 修改DialogActivity的继承关系,使之不要继承AppCompatActivity,继承Activity即可
    如果是在反编译的时候遇到这个问题,那么事情就不一样了。在本人第一次遇到这个问题的时候,发现Activity的继承关系,theme的主题索引完全没有问题,那么问题是什么呢?
    琢磨了好久,发现了AppCompatDelegateImpl这个类里的这段代码:
if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
    a.recycle();
    throw new IllegalStateException(
    "You need to use a Theme.AppCompat theme (or descendant) with this activity.");
    }

R.styleable.AppCompatTheme_windowActionBar
这个资源索引好像找不到,找了下发现确实找不到,我把反编译生成的资源文件都复制进去了,其中R.styleable.AppCompatTheme_windowActionBar所需的declare-styleable.xml的资源被转化成了attrs.xml里的attr标签了。
比如:AppCompatTheme的styleable被定义为:

<declare-styleable name="AppCompatTheme">
        <eat-comment/>
        <attr format="boolean" name="windowActionBar"/>
        <attr format="boolean" name="windowNoTitle"/>
        <attr format="boolean" name="windowActionBarOverlay"/>
        <attr format="boolean" name="windowActionModeOverlay"/>
        <attr format="dimension|fraction" name="windowFixedWidthMajor"/>
        <attr format="dimension|fraction" name="windowFixedHeightMinor"/>
        <attr format="dimension|fraction" name="windowFixedWidthMinor"/>
        <attr format="dimension|fraction" name="windowFixedHeightMajor"/>
        <attr format="dimension|fraction" name="windowMinWidthMajor"/>
        <attr format="dimension|fraction" name="windowMinWidthMinor"/>
        <attr name="android:windowIsFloating"/>
        <attr name="android:windowAnimationStyle"/>
        <eat-comment/>
        <attr format="reference" name="actionBarTabStyle"/>
        <attr format="reference" name="actionBarTabBarStyle"/>
        <attr format="reference" name="actionBarTabTextStyle"/>
        <attr format="reference" name="actionOverflowButtonStyle"/>
        <attr format="reference" name="actionOverflowMenuStyle"/>
        <attr format="reference" name="actionBarPopupTheme"/>
        <attr format="reference" name="actionBarStyle"/>
        <attr format="reference" name="actionBarSplitStyle"/>
        <attr format="reference" name="actionBarTheme"/>
        <attr format="reference" name="actionBarWidgetTheme"/>
        <attr format="dimension" name="actionBarSize">
            <enum name="wrap_content" value="0"/>
        </attr>
        <attr format="reference" name="actionBarDivider"/>
        <attr format="reference" name="actionBarItemBackground"/>
        <attr format="reference" name="actionMenuTextAppearance"/>
        <attr format="color|reference" name="actionMenuTextColor"/>
        <eat-comment/>
        <attr format="reference" name="actionModeStyle"/>
        <attr format="reference" name="actionModeCloseButtonStyle"/>
        <attr format="reference" name="actionModeBackground"/>
        <attr format="reference" name="actionModeSplitBackground"/>
        <attr format="reference" name="actionModeCloseDrawable"/>
        <attr format="reference" name="actionModeCutDrawable"/>
        <attr format="reference" name="actionModeCopyDrawable"/>
        <attr format="reference" name="actionModePasteDrawable"/>
        <attr format="reference" name="actionModeSelectAllDrawable"/>
        <attr format="reference" name="actionModeShareDrawable"/>
        <attr format="reference" name="actionModeFindDrawable"/>
        <attr format="reference" name="actionModeWebSearchDrawable"/>
        <eat-comment/>
        <attr format="reference" name="textAppearanceLargePopupMenu"/>
        <attr format="reference" name="textAppearanceSmallPopupMenu"/>
        <attr format="reference" name="textAppearancePopupMenuHeader"/>
        <attr format="reference" name="dialogTheme"/>
        <attr format="dimension" name="dialogPreferredPadding"/>
        <attr format="reference" name="listDividerAlertDialog"/>
        <attr format="dimension" name="dialogCornerRadius"/>
        <attr format="reference" name="actionDropDownStyle"/>
        <attr format="dimension" name="dropdownListPreferredItemHeight"/>
        <attr format="reference" name="spinnerDropDownItemStyle"/>
        <attr format="reference" name="homeAsUpIndicator"/>
        <attr format="reference" name="buttonBarStyle"/>
        <attr format="reference" name="buttonBarButtonStyle"/>
        <attr format="reference" name="selectableItemBackground"/>
        <attr format="reference" name="selectableItemBackgroundBorderless"/>
        <attr format="reference" name="borderlessButtonStyle"/>
        <attr format="reference" name="dividerVertical"/>
        <attr format="reference" name="dividerHorizontal"/>
        <attr format="reference" name="activityChooserViewStyle"/>
        <attr format="reference" name="toolbarStyle"/>
        <attr format="reference" name="toolbarNavigationButtonStyle"/>
        <attr format="reference" name="popupMenuStyle"/>
        <attr format="reference" name="popupWindowStyle"/>
        <attr format="reference|color" name="editTextColor"/>
        <attr format="reference" name="editTextBackground"/>
        <attr format="reference" name="imageButtonStyle"/>
        <eat-comment/>
        <attr format="reference" name="textAppearanceSearchResultTitle"/>
        <attr format="reference" name="textAppearanceSearchResultSubtitle"/>
        <attr format="reference|color" name="textColorSearchUrl"/>
        <attr format="reference" name="searchViewStyle"/>
        <eat-comment/>
        <attr format="dimension" name="listPreferredItemHeight"/>
        <attr format="dimension" name="listPreferredItemHeightSmall"/>
        <attr format="dimension" name="listPreferredItemHeightLarge"/>
        <attr format="dimension" name="listPreferredItemPaddingLeft"/>
        <attr format="dimension" name="listPreferredItemPaddingRight"/>
        <attr format="dimension" name="listPreferredItemPaddingStart"/>
        <attr format="dimension" name="listPreferredItemPaddingEnd"/>
        <attr format="reference" name="dropDownListViewStyle"/>
        <attr format="reference" name="listPopupWindowStyle"/>
        <attr format="reference" name="textAppearanceListItem"/>
        <attr format="reference" name="textAppearanceListItemSecondary"/>
        <attr format="reference" name="textAppearanceListItemSmall"/>
        <eat-comment/>
        <attr format="reference" name="panelBackground"/>
        <attr format="dimension" name="panelMenuListWidth"/>
        <attr format="reference" name="panelMenuListTheme"/>
        <attr format="reference" name="listChoiceBackgroundIndicator"/>
        <eat-comment/>
        <attr format="color" name="colorPrimary"/>
        <attr format="color" name="colorPrimaryDark"/>
        <attr format="color" name="colorAccent"/>
        <attr format="color" name="colorControlNormal"/>
        <attr format="color" name="colorControlActivated"/>
        <attr format="color" name="colorControlHighlight"/>
        <attr format="color" name="colorButtonNormal"/>
        <attr format="color" name="colorSwitchThumbNormal"/>
        <attr format="reference" name="controlBackground"/>
        <attr format="reference" name="alertDialogStyle"/>
        <attr format="reference" name="alertDialogButtonGroupStyle"/>
        <attr format="boolean" name="alertDialogCenterButtons"/>
        <attr format="reference" name="alertDialogTheme"/>
        <attr format="reference|color" name="textColorAlertDialogListItem"/>
        <attr format="reference" name="buttonBarPositiveButtonStyle"/>
        <attr format="reference" name="buttonBarNegativeButtonStyle"/>
        <attr format="reference" name="buttonBarNeutralButtonStyle"/>
        <eat-comment/>
        <attr format="reference" name="autoCompleteTextViewStyle"/>
        <attr format="reference" name="buttonStyle"/>
        <attr format="reference" name="buttonStyleSmall"/>
        <attr format="reference" name="checkboxStyle"/>
        <attr format="reference" name="checkedTextViewStyle"/>
        <attr format="reference" name="editTextStyle"/>
        <attr format="reference" name="radioButtonStyle"/>
        <attr format="reference" name="ratingBarStyle"/>
        <attr format="reference" name="ratingBarStyleIndicator"/>
        <attr format="reference" name="ratingBarStyleSmall"/>
        <attr format="reference" name="seekBarStyle"/>
        <attr format="reference" name="spinnerStyle"/>
        <attr format="reference" name="switchStyle"/>
        <attr format="reference" name="listMenuViewStyle"/>
        <attr format="reference" name="tooltipFrameBackground"/>
        <attr format="reference|color" name="tooltipForegroundColor"/>
        <attr format="reference|color" name="colorError"/>
        <attr format="string" name="viewInflaterClass"/>
        <eat-comment/>
        <attr format="reference" name="listChoiceIndicatorMultipleAnimated"/>
        <attr format="reference" name="listChoiceIndicatorSingleAnimated"/>
    </declare-styleable>

而我拿到的是反编译后的资源标识,declare-styleable标签被移除,name属性找不到,只有attr,那就肯定找不到R.styleable.AppCompatTheme_windowActionBar,所以报错了。。

解决办法

是重新拿到declare-styleables.xml,把declare-styleables.xml中定义的attr在attrs中删除,问题就解决了~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
============================================================== 兼容包实际上是为了让android 2.2、android 2.3 android 3.0支持android 4.0中才有的东西的。 比如说Actionbar。如果你不用兼容包的话,你开发含actionbar的程序就不可以在2.2上运行了 Support v11 v12 v13 v14 v17 Android 4.2.2 v18 Android 4.3.1 v21 Android 5.0.1 v22 Android 5.1.1 v23 Android 6.0 这个工程默认的是v21 Anroid 5.0.1, 如果需要编译(v22 Android 5.1.1 或者 v23 Android 6.0)版本,请 分别将values-v22 或者 values-v23 复制到 appcompat\res. 这是因为编译appcompat 需要SDK的部分资源,如果你需要编译v22版本的 appcompat-v7-v22.jar, 却将values-v23 复制到appcompat\res,那么会提示部分资源不存在,除非你选用v23的编译工具。 导入工程的方法见: http://blog.csdn.net/judyge/article/details/49228579 ============================================================== 出现这样的错误,说明你的工程缺乏appcompat V7 包 使用adt开发新建一个Android app,选择支持的SDK版本如果小于11(Android3.0)就会报如下错误。 error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'. 官网给出的答案是: https://developer.android.com/tools/support-library/setup.html#add-library 简单来说就是新的eclipse默认模版主题UI需要使用比较高版本api,如果需要支持低版本,需要导入appCompact库来支持,网上一般给出的解法: File->Import (android-sdk\extras\android\support\v7). Choose "appcompat" Project-> properties->Android. In the section library "Add" and choose "appCompat" 包括stackoverflow上也有很多人遇到,但很多人通过这个解决,但我就是没办法解决。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值