1

I am following this tutorial Flexibal Space with Image

For design pattern for a toolbar called Flexibal Space with Image in android. While using Palette here I am stuck in following error in java file causing application to crash.

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

Follwing is my java file 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
        }
    });

}
}

And xml file 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" />

And 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 ) --- > app theme

<!-- 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" />

(( Edit )) --> manifest file

<?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>

EDIT-->> Removed action bar from activity but same error occurred as shown in logcat enter image description here

Praval Sharma
  • 1,921
  • 2
  • 14
  • 20
  • 1
    You need to add this to your theme: false true – sanedroid Apr 21 '16 at 12:08
  • 1
    Your Stacktrace actually contains a pretty readable error message: `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.` – Jonas Köritz Apr 21 '16 at 12:08
  • @JonasKöritz what should I do to solve the problem – Praval Sharma Apr 21 '16 at 14:05
  • @PoojaGaikwad actually already provided you with the solution. – Jonas Köritz Apr 21 '16 at 14:06
  • @JonasKöritz I know she gave good solution but it didn't work for me – Praval Sharma Apr 21 '16 at 14:09
  • Your error message should have changed, the Error stated above still suggests you did not make the changes. – Jonas Köritz Apr 21 '16 at 14:09
  • I have made changes as you can see on screenshot added by me. I have removed action bar from activity. – Praval Sharma Apr 21 '16 at 14:21
  • dont remove actionbar from the activity.. just add the theme attribute mentioned in my answer to your tag in your manifest.. "AppTheme.NoActionBar".. Please update your code in the question after applying my changes if you still receive the error. – sanedroid Apr 22 '16 at 05:48

5 Answers5

6

R.attr.colorPrimary is the color ID. Try it like this:

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

or for API 23+ use:

ContextCompat.getColor(context, R.attr.colorPrimary);
Bene
  • 724
  • 8
  • 20
  • 1
    It is showing `getColor()` is deprecated and another error `Expected resource of type color` in `getColor()` – Praval Sharma Apr 21 '16 at 12:16
  • Just saw it's deprecated in Support Library 23. Try this: ContextCompat.getColor(context, R.color.your_color); – Bene Apr 21 '16 at 12:17
  • Added but now how to modify `collapsingToolbar.setContentScrimColor(mutedColor); ` as same error is showing in this line also. – Praval Sharma Apr 21 '16 at 12:30
  • mutedColor should now be set to the actual color, instead of the resource ID. I don't know what else could be wrong, sorry – Bene Apr 21 '16 at 12:38
1

Add this to your theme

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

Manifest:

<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>
sanedroid
  • 1,036
  • 4
  • 16
  • 27
  • you need add this "android:theme="@style/AppTheme.NoActionBar" inside you tag – sanedroid Apr 21 '16 at 12:46
  • Added but again app crashes. Actually problem is in java file in these lines `mutedColor = palette.getMutedColor(R.attr.colorPrimary);` and `collapsingToolbar.setContentScrimColor(mutedColor); ` inside `onGenerated()` method. – Praval Sharma Apr 21 '16 at 12:51
  • when you comment this lines and remove the theme tag I mentioned above.. does the app work?? Since your stacktrace is identifying a completely different error.. – sanedroid Apr 21 '16 at 12:56
  • I comment the lines but nothing happened. According to stacktrace `windowActionBar` should be `false` but it is already set as `false` – Praval Sharma Apr 21 '16 at 13:08
  • nothing happened means the app works in that case? according to your manifest you arent using the theme that sets windowActionBar=false anywhere.. Also how are you so sure about the error coming from the Palette line? do you have any logs for the same? or is it a syntax error? – sanedroid Apr 21 '16 at 13:20
  • int mutedColor = palette.getMutedColor(ContextCompat.getColor(MainActivity.this, R.color.md_deep_orange_700)); collapsingToolbar.setContentScrimColor(mutedColor); Try this.. didnt receive any error at my end.. – sanedroid Apr 21 '16 at 13:34
  • thanks error from java file has solved but error from action bar is not solved . – Praval Sharma Apr 21 '16 at 14:05
0

Try R.color.colorPrimary instead of R.attr.colorPrimary. Specify colors in colors.xml file in res/values

Manish Jangid
  • 185
  • 1
  • 2
  • 9
0

Declare the int as ColorInt as follows

@ColorInt int mutedColor

Sid
  • 141
  • 1
  • 5
0

I faced the exact same problem, and here is my solution for this,

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

Hope this helps!..

Update: I know it sounds funny/dumb, i just disabled the inspection, thats it no compile time error and no run time error. I just wanted to let you know guys.

basavaraj_S
  • 1,333
  • 2
  • 12
  • 20