【问题标题】:Android - Should pass resolved color instead of resource id here: `getResources().getColor(R.attr.colorPrimary)`Android - 应该在这里传递解析的颜色而不是资源 ID:`getResources().getColor(R.attr.colorPrimary)`
【发布时间】:2016-04-21 12:04:30
【问题描述】:

我正在关注这个教程Flexibal Space with Image

用于在 android 中名为 Flexibal Space with Image 的工具栏的设计模式。在这里使用Palette 时,我在java 文件中遇到以下错误,导致应用程序崩溃。

Should pass resolved color instead of resource id here: `getResources().getColor(R.attr.colorPrimary)`

Folwing 是我的 java 文件 YoutubeActivity.java

public class YoutubeActivity extends AppCompatActivity {

Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbar;
int mutedColor = R.attr.colorPrimary;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_youtube);

    toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
    setSupportActionBar(toolbar);

    collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setTitle("Praval Sharma");
    ImageView header = (ImageView) findViewById(R.id.header);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.header);
    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
        @Override
        public void onGenerated(Palette palette) {
            mutedColor = palette.getMutedColor(R.attr.colorPrimary); //error 
            collapsingToolbar.setContentScrimColor(mutedColor);      //error
        }
    });

}
}

还有xml文件activity_youtube.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="32dp"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <ImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/header"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />
        <android.support.v7.widget.Toolbar
            android:id="@+id/anim_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
    android:id="@+id/scrollableview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"
    android:src="@drawable/newfloatnew"
    app:layout_anchor="@+id/appbar"
    app:layout_anchorGravity="bottom|right|end" />

还有logcat

04-21 17:27:16.660 14865-14865/com.praval.healthfreak W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
04-21 17:27:16.664 14865-14865/com.praval.healthfreak E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.praval.healthfreak, PID: 14865
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.praval.healthfreak/com.praval.healthfreak.YoutubeActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5345)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
at com.praval.healthfreak.YoutubeActivity.onCreate(YoutubeActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5343)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441) 
at android.app.ActivityThread.access$900(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5345) 
at java.lang.reflect.Method.invokeNative(Native Method) 

( EDIT ) --- > 应用主题

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

((编辑)) -->清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.praval.healthfreak">

<!-- To access internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- To access accounts configured on device -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- To use account credentials -->
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </activity>
    <activity android:name=".Splash">

    </activity>
    <activity android:name=".Form">

    </activity>
    <activity android:name=".YoutubeActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".IndexPage"
        android:label="@string/title_activity_index_page"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name=".FoodEntry">

    </activity>
    <activity android:name=".NewIndex">

    </activity>
</application>

编辑-->>从活动中删除了操作栏,但发生了同样的错误,如 logcat 中所示

【问题讨论】:

  • 你需要将这个添加到你的主题中:falsetrue
  • 您的 Stacktrace 实际上包含一条非常易读的错误消息:This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
  • @JonasKöritz 我应该怎么做才能解决这个问题
  • @PoojaGaikwad 实际上已经为您提供了解决方案。
  • @JonasKöritz 我知道她给出了很好的解决方案,但对我没有用

标签: android android-toolbar android-support-design android-appcompat android-palette


【解决方案1】:

R.attr.colorPrimary 是颜色 ID。 试试这样:

mutedColor = palette.getMutedColor(getResources().getColor(R.attr.colorPrimary)); 
collapsingToolbar.setContentScrimColor(mutedColor); 

或 API 23+ 使用:

ContextCompat.getColor(context, R.attr.colorPrimary);

【讨论】:

  • 它显示 getColor() 已弃用,getColor() 中的另一个错误 Expected resource of type color
  • 刚刚看到它在支持库 23 中已被弃用。试试这个:ContextCompat.getColor(context, R.color.your_color);
  • 已添加,但现在如何修改collapsingToolbar.setContentScrimColor(mutedColor); ,因为此行中也显示相同的错误。
  • mutedColor 现在应该设置为实际颜色,而不是资源 ID。我不知道还有什么问题,对不起
【解决方案2】:

将此添加到您的主题

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

清单:

<activity android:name=".YoutubeActivity" 
android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

【讨论】:

  • 你需要在你的 标签中添加这个“android:theme="@style/AppTheme.NoActionBar”
  • 已添加但应用再次崩溃。实际上问题出在mutedColor = palette.getMutedColor(R.attr.colorPrimary); 和collapsingToolbar.setContentScrimColor(mutedColor); 这几行onGenerated() 方法中的java 文件中。
  • 当您评论此行并删除我上面提到的主题标签时.. 该应用程序是否有效?由于您的堆栈跟踪正在识别一个完全不同的错误..
  • 我评论了这些行,但什么也没发生。根据堆栈跟踪windowActionBar 应该是false 但它已经设置为false
  • 什么都没发生意味着应用程序在这种情况下可以工作吗?根据您的清单,您没有在任何地方使用设置 windowActionBar=false 的主题。您如何确定来自 Palette 行的错误?你有同样的日志吗?还是语法错误?
【解决方案3】:

尝试使用 R.color.colorPrimary 而不是 R.attr.colorPrimary。在 res/values 中的 colors.xml 文件中指定颜色

【讨论】:

  • 将attr 替换为color 后仍然存在错误。 Should pass resolved color instead of resource id here: getResources().getColor(R.color.colorPrimary)`
  • 还要检查在 colors.xml 文件中您将颜色指定为 &lt;resources&gt; &lt;color name="colorPrimary"&gt;#FFFFFF&lt;/color&gt; &lt;/resources&gt;
【解决方案4】:

如下声明int为ColorInt

@ColorInt int mutedColor

【讨论】:

    【解决方案5】:

    我遇到了完全相同的问题,这是我的解决方案,

    mutedColor = palette.getMutedColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary);); 
    collapsingToolbar.setContentScrimColor(mutedColor); 
    

    希望这会有所帮助!..

    更新:我知道这听起来很有趣/愚蠢,我只是禁用了检查,那就是它没有编译时错误,也没有运行时错误。我只是想让你们知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2015-10-28
      • 2011-12-11
      • 2014-02-12
      相关资源
      最近更新 更多