Anthea
Anthea

Reputation: 3809

Android Referencing style attributes - triggering ResourceNotFoundException

I create my own style attribute like this:

 <attr name="color_foreground" format="color|reference" />

and I give it a value in my theme like that:

    <item name="color_foreground">@color/blue</item>

If i access this in my layout (that has been set as a contentView)

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="?color_foreground"
>

it works like a charme. But if I do the same in an inflated Layout I receive an error:

        final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.myInflatedLayout, null);

the error Output:

Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010005 a=-1}
    06-27 20:33:37.340: E/AndroidRuntime(31616):    at android.content.res.Resources.loadDrawable(Resources.java:1899)
    06-27 20:33:37.340: E/AndroidRuntime(31616):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
    06-27 20:33:37.340: E/AndroidRuntime(31616):    at android.view.View.<init>(View.java:2810)
    06-27 20:33:37.340: E/AndroidRuntime(31616):    at android.widget.TextView.<init>(TextView.java:561)
    06-27 20:33:37.340: E/AndroidRuntime(31616):    at android.widget.TextView.<init>(TextView.java:554)
    06-27 20:33:37.340: E/AndroidRuntime(31616):    ... 48 more

Binary XML Line 15 is actually the line where i access the attr via "?color_foreground"

Nearly the same happens if I try to get the attribute like that (right after setting the content view) :

this.getResources().getColor(R.attr.color_foreground);

the error:

06-28 12:55:16.975: E/AndroidRuntime(7089): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f010005
06-28 12:55:16.975: E/AndroidRuntime(7089):     at android.content.res.Resources.getValue(Resources.java:1019)
06-28 12:55:16.975: E/AndroidRuntime(7089):     at android.content.res.Resources.getColor(Resources.java:749)
06-28 12:55:16.975: E/AndroidRuntime(7089):     at ***.MyActivity.onCreate(AppointmentListActivity.java:66)
06-28 12:55:16.975: E/AndroidRuntime(7089):     at android.app.Activity.performCreate(Activity.java:4465)
06-28 12:55:16.975: E/AndroidRuntime(7089):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
06-28 12:55:16.975: E/AndroidRuntime(7089):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
06-28 12:55:16.975: E/AndroidRuntime(7089):     ... 11 more

Upvotes: 1

Views: 2096

Answers (3)

Samoka
Samoka

Reputation: 1333

In case someone might encounter this problem again. Change

android:background="?color_foreground"

to

style="?color_foreground"

and in theme you can specify (if you have multiple themes, you will add this one by one in every theme, but different color/style, depends on your design)

<item name="color_foreground">@color/blue</item>

or

<item name="color_foreground">@style/blue</item>

and in your style

<style name="blue">
    <item name="android:background">#0000FF</item>
</style>

Upvotes: -1

Anthea
Anthea

Reputation: 3809

The problem was, that I worked on the context of the activity. If I work on the activity itself it don't cause any problem.

Upvotes: 2

MH.
MH.

Reputation: 45493

Resources$NotFoundException: Resource is not a Drawable (color or path)

From the looks of that exception, your custom attribute is not being recognised. You probably need to do one or both of the following:

  1. Change the reference to android:background="?attr/color_foreground".
  2. In the LinearLayout definition, add a reference to the namespace that defines your custom attribute, which is just the package name of your project: xmlns:android="http://schemas.android.com/apk/res/com.my.package.name"

My guess is that the first suggestion will be sufficient, but just in case, you may like to try the second too.

Upvotes: 0

Related Questions