"您需要使用theme.appcompat主题"以Theme.AppCompat为主题,Sdk v23

时间:2015-08-20 12:46:10

标签: android android-appcompat android-theme

配置:

Android Studio 1.4 Preview 2
compileSdkVersion 23
buildToolsVersion '23'
minSdkVersion 15
targetSdkVersion 23
Up-to-date support libraries (v23)

错误:[已更新]

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.android.debug/com.myapp.mobile.activities.HomeActivity}: 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.ActivityThread$H.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)
 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.k.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.myapp.mobile.activities.BaseActivity.onCreate(BaseActivity.java:47)
        at com.myapp.mobile.activities.HomeActivity.onCreate(HomeActivity.java:57)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        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.ActivityThread$H.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)

我的应用程序在切换到v23之前运行良好(之前的配置是v22并支持库v22.2.1。升级后,编译正确完成,但此错误仅在启动时出现。

我在SO上看过很多帖子,问题来自于一个糟糕的造型,还有一个Holo的父母。棘手的问题发现它们来自Manifest文件,其中一个活动主题覆盖了app主题。

这是我的情况,所以我将测试目的改为:

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/Theme.BaseMyApp">

    <!-- Launch activity. MyApp.Theme has Theme.BaseMyApp for parent, but has 2 more attributes for v21 -->
    <activity
        android:name=".activities.HomeActivity"
        android:launchMode="singleTop"
        android:theme="@style/MyApp.Theme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

...但是,有多奇怪,错误仍然存​​在!我也检查了活动布局的视图,但似乎没有什么问题,只有一个主题扩展AppCompat的工具栏:

toolbar_layout.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.Toolbar"
    app:subtitleTextAppearance="@style/AppTheme.Toolbar.SubTitle"
    app:titleTextAppearance="@style/AppTheme.Toolbar.Title">

    <ProgressBar
        android:id="@+id/progress_spinner"
        style="@style/Widget.MyApp.ActionBar.Progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:indeterminate="true"
        android:visibility="gone" />
</android.support.v7.widget.Toolbar>

,其中

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.ActionBar">
    <!-- Customize color of navigation drawer icon and back arrow -->
    <item name="colorControlNormal">@android:color/white</item>
</style>

<style name="AppTheme.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <!-- Set proper title size -->
    <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item>
    <!-- Set title color -->
    <item name="android:textColor">@android:color/white</item>
</style>

<style name="AppTheme.Toolbar.SubTitle" parent="TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
    <!-- Set proper title size -->
    <item name="android:textSize">@dimen/abc_text_size_subtitle_material_toolbar</item>
    <!-- Set title color -->
    <item name="android:textColor">@android:color/white</item>
</style>


<style name="Widget.MyApp.ActionBar.Progress" parent="Widget.AppCompat.ProgressBar">
    <item name="android:minHeight">25dp</item>
    <item name="android:maxHeight">25dp</item>
</style>

你知道我搞砸了吗?

由于

2 个答案:

答案 0 :(得分:6)

问题部分解决了。

罪行是:

at android.support.v7.app.k.onCreate(AppCompatDelegateImplBase.java:124)

我使用these separate ProGuard files配置ProGuard。也许我没有指向正确的/app/proguard目录。因此,ProGuard忽略了myapp-rules.pro我要说的那些android.support.v7.**类。

修好后,我收到了错误:

:app:proguardDebug
Warning: com.paypal.android.sdk.aT: can't find referenced field 'org.apache.http.HttpEntity wrappedEntity' in program class com.paypal.android.sdk.aT
Warning: uk.co.senab.photoview.PhotoViewAttacher: can't find referenced method 'float sqrt(float)' in library class android.util.FloatMath
Warning: uk.co.senab.photoview.gestures.CupcakeGestureDetector: can't find referenced method 'float sqrt(float)' in library class android.util.FloatMath
Warning: there were 1 unresolved references to program class members.
    Your input classes appear to be inconsistent.
    You may need to recompile the code.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
Warning: there were 2 unresolved references to library class members.
     You probably need to update the library versions.
     (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedlibraryclassmember)
Exception while processing task java.io.IOException: Please correct the above warnings first.
    at proguard.Initializer.execute(Initializer.java:473)
    at proguard.ProGuard.initialize(ProGuard.java:233)
    at proguard.ProGuard.execute(ProGuard.java:98)
    at proguard.gradle.ProGuardTask.proguard(ProGuardTask.java:1074)
    at com.android.build.gradle.tasks.AndroidProGuardTask.doMinification(AndroidProGuardTask.java:139)
    at com.android.build.gradle.tasks.AndroidProGuardTask$1.run(AndroidProGuardTask.java:115)
    at com.android.builder.tasks.Job.runTask(Job.java:48)
    at com.android.build.gradle.tasks.SimpleWorkQueue$EmptyThreadContext.runTask(SimpleWorkQueue.java:41)
    at com.android.builder.tasks.WorkQueue.run(WorkQueue.java:227)
    at java.lang.Thread.run(Thread.java:745)
:app:dexDebug
:app:crashlyticsStoreDeobsDebug
:app:crashlyticsUploadDeobsDebug
:app:validateDebugSigning
:app:packageDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:packageDebug'.
> Unable to compute hash of
/home/guillaume/workspace/MyApp/app/build/intermediates/classes-proguard/debug/classes.jar

所以我通过添加规则来修复警告

-dontwarn com.paypal.android.sdk.**
-dontwarn uk.co.senab.photoview.**

然而问题似乎更深,因为PhotoView和Braintree / Card.io(也是PayPal SDK)使用API​​ 23删除的方法。球不在我的计算中,是吗?

答案 1 :(得分:0)

感谢您的更新。也许您的工具栏父级是问题。它有另一个继承而不是Theme.AppCompat。

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.ActionBar">
  <!-- Customize color of navigation drawer icon and back arrow -->
  <item name="colorControlNormal">@android:color/white</item>
</style>

尝试将此用于工具栏:

"Widget.AppCompat.Toolbar"

作为父母