3

I am receiving the following messages in my console:

Error:(15, 22) No resource found that matches the given name: attr 'colorAccent'. Error:(13, 22)

No resource found that matches the given name: attr 'colorPrimary'. Error:(14, 22)

No resource found that matches the given name: attr 'colorPrimaryDark'.

This is my style.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppBaseTheme" parent="android:Theme.Holo">

        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_secondary</item>
        <item name="colorAccent">@color/color_accent</item>
    </style>
</resources>

my color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="cyan">#6441a5</color>
    <color name="color_primary">#6441a5</color>
    <color name="color_secondary">#6441a5</color>
    <color name="color_accent">#6441a5</color>

</resources>

After searching and viewing many other solutions most answer said to change the target SDK to 21: Here is my manifest file:

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="21" />

And my build grade:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    compileOptions.encoding = 'utf-8'

    defaultConfig {
        applicationId "com.test.test"
        minSdkVersion 15
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:21.0.0'
}

But this still hasn't worked. What could be the issue here?

J.Doe
  • 31
  • 5

1 Answers1

3

If you are using the colorPrimary, colorPrimaryDark, and colorAccent attributes without the android: namespace, this indicates that you are using the support library.

I would check to see if you have added the v7 support library in your dependencies, and make sure that your app theme has a parent of one of the Theme.AppCompat styles.

This also means you need to add compile 'com.android.support:appcompat-v7:21.+' into your Gradle dependencies and your Activity files must extend AppCompatActivity.


Your relevant files should look something like this (I have used the reference/attribute names as you put in your answer):

AndroidManifest.xml:

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

    ...

    <application
        ...
        android:theme="@style/AppBaseTheme" >

    ...

styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_secondary</item>
        <item name="colorAccent">@color/color_accent</item>
    </style>

    ...

colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="color_primary">#6441a5</color>
    <color name="color_secondary">#6441a5</color>
    <color name="color_accent">#6441a5</color>

    ...

YourActivity.java:

....

public class DetailActivity extends AppCompatActivity {

....

}

build.gradle:

...

dependencies {
    ...
    compile 'com.android.support:appcompat-v7:21.+'
    ...
}

Make sure you have included the lines I have listed above.

Of course, where I have put ... indicates that you may have put other code there, so don't literally put ... into your code.


Also, note that if you are using Gradle, the version/API declarations in the manifest are irrelevant. Therefore, you should be able to remove this without any errors occurring:

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="21" />
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
  • I tried this: @color/color_primary @color/color_secondary @color/color_accent the app started but did not change to color – J.Doe Aug 29 '15 at 20:26
  • 1
    @J.Doe you should set the parent of your AppBaseTheme to "Theme.AppCompat" like Fabod mentioned – Pfennigbaum Aug 29 '15 at 20:27
  • I get an error if I put it like this: – J.Doe Aug 29 '15 at 20:30
  • @J.Doe You do not need `android:` in the theme name. – Farbod Salamat-Zadeh Aug 29 '15 at 20:30
  • didn't I? – J.Doe Aug 29 '15 at 20:32
  • @J.Doe What exactly is the error? Also, note that your activity needs to extend `AppCompatActivity` and you need to have `compile 'com.android.support:appcompat-v7:21.+'` in your gradle dependencies. – Farbod Salamat-Zadeh Aug 29 '15 at 20:39
  • ok I added the compile thing. Unfortunaly my MainActivity cannot extends AppCombatActivity MainActivity:public class MainActivity extends FragmentActivity { It already extends somethings. – J.Doe Aug 29 '15 at 20:47
  • @J.Doe As your minimum SDK version is 15, you should be able to extend `AppCompatActivity` without any issues. – Farbod Salamat-Zadeh Aug 29 '15 at 20:49
  • hmm I do not get it sorry?I though it is not possible to multiple inheritance in java.My MainActivity already extends FragmentActivity – J.Doe Aug 29 '15 at 20:53
  • @J.Doe Sorry, what I meant is that you should get rid of the `FragmentActivity` and instead extend `AppCompatActivity`. As in the [Android documentation](http://developer.android.com/training/basics/fragments/creating.html#AddInLayout). This is because `AppCompatActivity` is a subclass of `FragmentActivity` (see [`AppCompatActivity` documentation](http://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html)). – Farbod Salamat-Zadeh Aug 29 '15 at 20:57
  • Ok but I still cannot extends AppCompatActivity. Cannot resolve symbol of AppCompatActivity – J.Doe Aug 29 '15 at 20:59
  • @J.Doe Have a look at [a sort of checklist for using AppCompat from the Android developers blog](http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-pre.html). If you still run in to errors, post the exact error and the line causing the error. – Farbod Salamat-Zadeh Aug 29 '15 at 21:04
  • @J.Doe Hmm. What errors are you getting and what do they point to? – Farbod Salamat-Zadeh Aug 29 '15 at 22:04
  • @J.Doe That's strange... Is your activity using that theme (specificied using `android:theme` in the Manifest)? Also, edit your question by posting the code you changed and other relevant code, and I'll look at it in the morning (it's quite late for me here ;) ). – Farbod Salamat-Zadeh Aug 29 '15 at 22:31
  • @J.Doe Have a look at my updated answer. Make sure you have all the relevant lines of code and let me know if its still not working and what exactly the problem/error is. – Farbod Salamat-Zadeh Aug 30 '15 at 09:19
  • ty I tried this, but did not work I also changed the getActionBar() method to getSupportActionBar() and this line:: actionBar.addTab(actionBar.newTab().setText("November").setTabListener((ActionBar.TabListener) tabListener)); to: actionBar.addTab(actionBar.newTab().setText("November").setTabListener((android.support.v7.app.ActionBar.TabListener) tabListener)); – J.Doe Aug 30 '15 at 15:51
  • @J.Doe Is it now the tabs that are causing the issue, or is it the theme still? I am determined to fix this for you :P – Farbod Salamat-Zadeh Aug 30 '15 at 16:13
  • The app does not start anymore and I get following message: java.lang.ClassCastException: com.test.tab.MainActivity$2 cannot be cast to android.support.v7.app.ActionBar$TabListener and: Caused by: java.lang.ClassCastException: com.testtab.MainActivity$2 cannot be cast to android.support.v7.app.ActionBar$TabListener at com.testtab.MainActivity.onCreate(MainActivity.java:84) – J.Doe Aug 30 '15 at 16:15
  • @J.Doe I know how to fix that. That error would be because you cannot use a v7 support `ActionBar` with tabs - it is deprecated I think. In fact, I wrote a [question and answer here on SO](http://stackoverflow.com/questions/29868277/using-sliding-tabs-with-toolbar/29991826#29991826) to solve this issue. This means you need to switch from using `ActionBar` to `Toolbar` and updating your app to use these libraries and new components is really worth it as you can make use of these new features. Sorry for taking you through all this trouble for such a small issue. – Farbod Salamat-Zadeh Aug 30 '15 at 16:29
  • Thank you for helping I will have a look at it. – J.Doe Aug 30 '15 at 16:35